Phpmemcached cache code instance

Source: Internet
Author: User
Phpmemcached cache code instance

  1. Class MemcacheModel {
  2. Private $ mc = null;
  3. /**
  4. * Constructor is used to add a server and create a memcahced object.
  5. */
  6. Function _ construct (){
  7. $ Params = func_get_args ();
  8. $ Mc = new Memcache;
  9. // If multiple memcache servers exist
  10. If (count ($ params)> 1 ){
  11. Foreach ($ params as $ v ){
  12. Call_user_func_array (array ($ mc, 'addserver'), $ v );
  13. }
  14. // If there is only one memcache server
  15. } Else {
  16. Call_user_func_array (array ($ mc, 'addserver'), $ params [0]);
  17. }
  18. $ This-> mc = $ mc;
  19. }
  20. /**
  21. * Get the memcached object
  22. * @ Return object memcached object
  23. */
  24. Function getMem (){
  25. Return $ this-> mc;
  26. }
  27. /**
  28. * Check whether mem connection is successful.
  29. * @ Return true if the bool connection is successful; otherwise, false is returned.
  30. */
  31. Function mem_connect_error (){
  32. $ Stats = $ this-> mc-> getStats ();
  33. If (emptyempty ($ stats )){
  34. Return false;
  35. } Else {
  36. Return true;
  37. }
  38. }
  39. Private function addKey ($ tabName, $ key ){
  40. $ Keys = $ this-> mc-> get ($ tabName );
  41. If (emptyempty ($ keys )){
  42. $ Keys = array ();
  43. }
  44. // If the key does not exist, add
  45. If (! In_array ($ key, $ keys )){
  46. $ Keys [] = $ key; // add the new key to the keys of the table
  47. $ This-> mc-> set ($ tabName, $ keys, MEMCACHE_COMPRESSED, 0 );
  48. Return true; // return true if no data exists.
  49. } Else {
  50. Return false; // If yes, false is returned.
  51. }
  52. }
  53. /**
  54. * Add data to memcache
  55. * @ Param string $ tabName: name of the table whose data table needs to be cached
  56. * @ Param string $ SQL uses SQL as the memcache key
  57. * @ Param mixed $ data the data to be cached
  58. */
  59. Function addCache ($ tabName, $ SQL, $ data ){
  60. $ Key = md5 ($ SQL );
  61. // If it does not exist
  62. If ($ this-> addKey ($ tabName, $ key )){
  63. $ This-> mc-> set ($ key, $ data, MEMCACHE_COMPRESSED, 0 );
  64. }
  65. }
  66. /**
  67. * Get the data stored in memcahce
  68. * @ Param string $ SQL uses the SQL key
  69. * @ Return mixed return the cached data
  70. */
  71. Function getCache ($ SQL ){
  72. $ Key = md5 ($ SQL );
  73. Return $ this-> mc-> get ($ key );
  74. }
  75. /**
  76. * Delete all caches related to the same table
  77. * @ Param string $ tabName table name
  78. */
  79. Function delCache ($ tabName ){
  80. $ Keys = $ this-> mc-> get ($ tabName );
  81. // Delete all the caches of the same table
  82. If (! Emptyempty ($ keys )){
  83. Foreach ($ keys as $ key ){
  84. $ This-> mc-> delete ($ key, 0); // 0 indicates immediate deletion
  85. }
  86. }
  87. // Delete the keys of all SQL statements in the table
  88. $ This-> mc-> delete ($ tabName, 0 );
  89. }
  90. /**
  91. * Delete the cache of a separate statement
  92. * @ Param string $ SQL statement executed
  93. */
  94. Function delone ($ SQL ){
  95. $ Key = md5 ($ SQL );
  96. $ This-> mc-> delete ($ key, 0); // 0 indicates immediate deletion
  97. }
  98. }
  99. ?>

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.