Php Data file cache code example-php Tutorial

Source: Internet
Author: User
Php Data file cache code instance

  1. Class DataCache
  2. {
  3. /**
  4. * Array conversion
  5. *
  6. * @ Param array $ array
  7. * @ Param string $ arrayName
  8. * @ Param array $ level
  9. *
  10. * @ Return string
  11. */
  12. Private function arrayEval ($ array, $ arrayName = '', $ level = 0)
  13. {
  14. $ Space = str_repeat ("t", $ level );
  15. If (emptyempty ($ arrayName ))
  16. {
  17. $ Evaluate = "arrayn $ space (n ";
  18. }
  19. Else
  20. {
  21. $ Evaluate = "$ {$ arrayName} = arrayn $ space (n ";
  22. }
  23. $ Space2 = str_repeat ("t", $ level + 1 );
  24. $ Comma = $ space2;
  25. If (! Emptyempty ($ array ))
  26. {
  27. Foreach ($ array as $ key => $ val)
  28. {
  29. $ Key = is_string ($ key )? '''. Addcslashes ($ key, ''\ '). ''': $ key;
  30. $ Val =! Is_array ($ val )&&(! Preg_match ('/^ -? [1-9] d * $/', $ val) | strlen ($ val)> 12 )? '''. Addcslashes ($ val, ''\ '). ''': $ val;
  31. If (is_array ($ val ))
  32. {
  33. $ Evaluate. = "$ comma $ key =>". arrayEval ($ val, '', $ level + 1 );
  34. }
  35. Else
  36. {
  37. $ Evaluate. = "$ comma $ key => $ val ";
  38. }
  39. $ Comma = ", n $ space2 ";
  40. }
  41. }
  42. $ Evaluate. = "n $ space )";
  43. // A ";" is required at the end.
  44. If ($ level = 0)
  45. {
  46. $ Evaluate. = ";";
  47. }
  48. Return $ evaluate;
  49. }
  50. /**
  51. * Write cache
  52. *
  53. * @ Param string $ path
  54. * @ Param string $ arrayName
  55. * @ Param array $ data
  56. *
  57. * @ Return boolean
  58. */
  59. Public static function writeCache ($ path, $ arrayName, $ data)
  60. {
  61. If ($ handle = fopen ($ path, 'W + '))
  62. {
  63. $ Data = self: arrayEval ($ data, $ arrayName );
  64. $ DataConvert ="
  65. Flock ($ handle, LOCK_EX );
  66. $ Rs = fputs ($ handle, $ dataConvert );
  67. Flock ($ handle, LOCK_UN );
  68. Fclose ($ handle );
  69. If ($ rs! = False)
  70. {
  71. Return true;
  72. }
  73. }
  74. Return false;
  75. }
  76. }
  77. ?>

Call method:

  1. /**
  2. * Generate file cache
  3. *
  4. * @ Param string $ filePath: Path to save the cached file
  5. * @ Param string $ array name in the cache file
  6. * @ Param array $ data
  7. *
  8. * @ Return boolean
  9. */
  10. DataCache: writeCache ($ filePath, $ arrayName, $ data );

Memcache to cache data. the file cache class:

  1. /**
  2. * File cache class
  3. * File caching
  4. */
  5. Class Cache_FileCache {
  6. /**
  7. * Set cache
  8. * @ Param $ key the key cached by the key
  9. * @ Param $ data cached content
  10. * @ Param $ cacheLife cache time (unit: Seconds) if it is 0, it indicates unlimited time
  11. * @ Return Bool
  12. */
  13. Public static function setCache ($ key, $ data, $ cacheLife)
  14. {
  15. If (file_exists (_ SITE_FILE_CACHE ))
  16. {
  17. @ $ File = _ SITE_FILE_CACHE. "/". $ key. ". php ";
  18. $ Cache = array ();
  19. $ Time = _ SYS_TIME;
  20. $ Cache ['content'] = $ data;
  21. $ Cache ['expire '] = $ cacheLife = 0? 0: $ time + $ cacheLife;
  22. $ Cache ['mtime'] = $ time;
  23. $ Cache = serialize ($ cache );
  24. $ SetReslut = @ file_put_contents ($ file, $ cache) or self: error (_ line __, "file write error ");
  25. $ ChmodReslut = @ chmod ($ file, 0777) or self: error (_ line __, "failed to set file permissions ");
  26. If ($ setReslut & $ chmodReslut)
  27. {
  28. Return true;
  29. }
  30. Else
  31. {
  32. Return false;
  33. }
  34. }
  35. }
  36. /**
  37. * Get cached data
  38. * @ Param $ key the key cached by the key
  39. * @ Return array
  40. */
  41. Public static function getCache ($ key)
  42. {
  43. @ $ File = _ SITE_FILE_CACHE. "/". $ key. ". php ";
  44. If (file_exists ($ file ))
  45. {
  46. $ Data = @ file_get_contents ($ file );
  47. $ Data = unserialize ($ data );
  48. If ($ data ['expire '] = 0 | $ data ['expire']> _ SYS_TIME)
  49. {
  50. Return $ data ['content'];
  51. }
  52. Else
  53. {
  54. Unlink ($ file );
  55. Return array ();
  56. }
  57. }
  58. }
  59. /**
  60. * Deleting cached files
  61. * @ Param $ key cache $ key
  62. * @ Return Bool
  63. */
  64. Public static function delCache ($ key)
  65. {
  66. If (@ unlink (_ SITE_FILE_CACHE. "/". $ key. ". php "))
  67. {
  68. Return true;
  69. }
  70. Else
  71. {
  72. Return false;
  73. }
  74. }
  75. /**
  76. * Clear all cached files
  77. * @ Return Bool
  78. */
  79. Public static function clearAllCache ()
  80. {
  81. $ Files = scandir (_ SITE_FILE_CACHE );
  82. Foreach ($ files as $ val)
  83. {
  84. @ Unlink (_ SITE_FILE_CACHE. "/". $ val );
  85. }
  86. }
  87. /**
  88. * Error handling functions
  89. * @ Param $ line: number of rows
  90. * @ Param $ msg information
  91. */
  92. Public static function error ($ line, $ msg)
  93. {
  94. Die ("error file:". _ file _. "/n error line: $ line/n error message: $ msg ");
  95. }
  96. }
  97. ?>

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.