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

Source: Internet
Author: User
A cache interface demo written in php, compatible with redis and memcache

  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 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 incrementing ';
  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 'get redis value ';
  53. }
  54. Public function delVal ($ key)
  55. {
  56. Echo 'redis deletion value ';
  57. }
  58. Public function autoIncreament ($ key)
  59. {
  60. Echo 'redis auto-incrementing ';
  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 ']; // determines the null value.
  74. Self: $ conf = $ _ SC ['cacheconf']; // determines the null value.
  75. }
  76. // Create a cache object by using an external call
  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. ?>

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.