Home  >  Article  >  Backend Development  >  A cache interface demo written in PHP, compatible with redis and memcache

A cache interface demo written in PHP, compatible with redis and memcache

WBOY
WBOYOriginal
2016-07-25 09:03:541057browse
  1. /**

  2. * Factory method pattern
  3. * -------------
  4. * @author zhangqian
  5. * @version v1.0
  6. */
  7. //Cache interface
  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 connection
  18. public function init($conf)
  19. {
  20. echo 'initialize mymem';
  21. }
  22. public function setVal($key , $val)
  23. {
  24. echo 'mem set value';
  25. }
  26. public function getVal($key)
  27. {
  28. echo 'mem get value';
  29. }
  30. public function delVal($key)
  31. {
  32. echo 'mem delete value';
  33. }
  34. public function autoIncreament($key)
  35. {
  36. echo 'mem auto-increment';
  37. }
  38. }
  39. //redis
  40. class redisCache implements cache {
  41. //redis connection
  42. public function init($arr)
  43. {
  44. echo 'Initialize redis';
  45. }
  46. public function setVal($key, $val)
  47. {
  48. echo 'redis set value';
  49. }
  50. public function getVal($key)
  51. {
  52. echo 'redis get value';
  53. }
  54. public function delVal($key)
  55. {
  56. echo 'redis delete value';
  57. }
  58. public function autoIncreament($key)
  59. {
  60. echo 'redis auto-increment';
  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');Load the configuration file to obtain the cache type and cache configuration parameters
  72. global $_SC;
  73. self ::$type = $_SC['cachetype'];//Judgment of short value
  74. self::$conf = $_SC['cacheconf'];//Judgment of short value
  75. }
  76. //External call to create cache object
  77. public static function CreateOperation(){
  78. self::getConfig();
  79. try
  80. {
  81. $error = 'Unknown cache type';
  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);//Connect
  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. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn