php寫的一個緩衝介面demo,相容redis和memcache

來源:互聯網
上載者:User
  1. /**

  2. * Factory 方法模式
  3. * -------------
  4. * @author zhangqian
  5. * @version v1.0
  6. */
  7. //緩衝介面
  8. interface cache {
  9. public function init($conf);
  10. public function setVal($key , $val);
  11. public function getVal($key);
  12. public function delVal($key);
  13. public function autoIncreament($key);
  14. }
  15. //mem
  16. class mymemCache implements cache {
  17. //mymem串連
  18. public function init($conf)
  19. {
  20. echo '初始化mymem';
  21. }
  22. public function setVal($key , $val)
  23. {
  24. echo 'mem設定值';
  25. }
  26. public function getVal($key)
  27. {
  28. echo 'mem擷取值';
  29. }
  30. public function delVal($key)
  31. {
  32. echo 'mem刪除值';
  33. }
  34. public function autoIncreament($key)
  35. {
  36. echo 'mem自增';
  37. }
  38. }
  39. //redis
  40. class redisCache implements cache {
  41. //redis串連
  42. public function init($arr)
  43. {
  44. echo '初始化redis';
  45. }
  46. public function setVal($key , $val)
  47. {
  48. echo 'redis設定值';
  49. }
  50. public function getVal($key)
  51. {
  52. echo 'redis擷取值';
  53. }
  54. public function delVal($key)
  55. {
  56. echo 'redis刪除值';
  57. }
  58. public function autoIncreament($key)
  59. {
  60. echo 'redis自增';
  61. }
  62. }
  63. class cacheFactory
  64. {
  65. private static $obj;
  66. private static $type;
  67. private static $conf;
  68. private static $allowtype = array('mymem','redis');
  69. private static function getConfig()
  70. {
  71. //include_once('config.php');載入設定檔 擷取緩衝的類型 及緩衝的配置參數
  72. global $_SC;
  73. self::$type = $_SC['cachetype'];//做空值的判斷
  74. self::$conf = $_SC['cacheconf'];//做空值的判斷
  75. }
  76. //外部調用建立緩衝對象
  77. public static function CreateOperation(){
  78. self::getConfig();
  79. try
  80. {
  81. $error = '未知的緩衝類型';
  82. if(in_array(self::$type , self::$allowtype))
  83. {
  84. $type = self::$type.'Cache';
  85. self::$obj = new $type;
  86. self::$obj->init(self::$conf);//串連
  87. }
  88. else
  89. throw new Exception($error);
  90. }
  91. catch (Exception $e) {
  92. echo 'Caught exception: ', $e->getMessage(), "\n";
  93. exit;
  94. }
  95. return self::$obj;
  96. }
  97. }

  98. $_SC = array();

  99. $_SC['cachetype'] = 'redis';//mymem
  100. $_SC['cacheconf'] = array();
  101. $cache = cacheFactory::CreateOperation();
  102. $cache->setVal('a',1);
  103. echo '
    ';
  104. $a = $cache->getVal('a');
  105. echo '
    ';
  106. $cache->delVal('a');
  107. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.