PHP writes a cache interface demo, compatible with Redis and memcache

Source: Internet
Author: User
  1. /**

  2. * Factory Method mode
  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 setting value ';
  25. }
  26. Public Function Getval ($key)
  27. {
  28. Echo ' mem gets value ';
  29. }
  30. Public Function DelVal ($key)
  31. {
  32. Echo ' mem Delete value ';
  33. }
  34. Public Function autoincreament ($key)
  35. {
  36. Echo ' mem self-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 setting 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 self-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 configuration file Gets the type of cache and the configuration parameters of the cache
  72. Global $_SC;
  73. Self:: $type = $_sc[' CacheType '];//the judgment of the short value
  74. Self:: $conf = $_sc[' cacheconf '];//the judgment of the 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);//Connection
  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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.