CacheTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace SleekDB\Traits;
  3. /**
  4. * Methods required to perform the cache mechanishm.
  5. */
  6. trait CacheTrait {
  7. /**
  8. * Make cache deletes the old cache if exists then creates a new cache file.
  9. * returns the data.
  10. * @return array
  11. */
  12. private function reGenerateCache() {
  13. $token = $this->getCacheToken();
  14. $result = $this->findStoreDocuments();
  15. // Write the cache file.
  16. file_put_contents( $this->getCachePath( $token ), json_encode( $result ) );
  17. // Reset cache flags to avoid future queries on the same object of the store.
  18. $this->resetCacheFlags();
  19. // Return the data.
  20. return $result;
  21. }
  22. /**
  23. * Use cache will first check if the cache exists, then re-use it.
  24. * If cache dosent exists then call makeCache and return the data.
  25. * @return array
  26. */
  27. private function useExistingCache() {
  28. $token = $this->getCacheToken();
  29. // Check if cache file exists.
  30. if ( file_exists( $this->getCachePath( $token ) ) ) {
  31. // Reset cache flags to avoid future queries on the same object of the store.
  32. $this->resetCacheFlags();
  33. // Return data from the found cache file.
  34. return json_decode( file_get_contents( $this->getCachePath( $token ) ), true );
  35. } else {
  36. // Cache file was not found, re-generate the cache and return the data.
  37. return $this->reGenerateCache();
  38. }
  39. }
  40. /**
  41. * This method would make a unique token for the current query.
  42. * We would use this hash token as the id/name of the cache file.
  43. * @return string
  44. */
  45. private function getCacheToken() {
  46. $query = json_encode( [
  47. 'store' => $this->storePath,
  48. 'limit' => $this->limit,
  49. 'skip' => $this->skip,
  50. 'conditions' => $this->conditions,
  51. 'orConditions' => $this->orConditions,
  52. 'in' => $this->in,
  53. 'notIn' => $this->notIn,
  54. 'order' => $this->orderBy,
  55. 'search' => $this->searchKeyword,
  56. 'fieldsToSelect' => $this->fieldsToSelect,
  57. 'fieldsToExclude' => $this->fieldsToExclude,
  58. 'orConditionsWithAnd' => $this->orConditionsWithAnd,
  59. ] );
  60. return md5( $query );
  61. }
  62. /**
  63. * Reset the cache flags so the next database query dosent messedup.
  64. */
  65. private function resetCacheFlags() {
  66. $this->makeCache = false;
  67. $this->useCache = false;
  68. }
  69. /**
  70. * Returns the cache directory absolute path for the current store.
  71. * @param string $token
  72. * @return string
  73. */
  74. private function getCachePath( $token ) {
  75. return $this->storePath . 'cache/' . $token . '.json';
  76. }
  77. /**
  78. * Delete a single cache file for current query.
  79. */
  80. private function _deleteCache() {
  81. $token = $this->getCacheToken();
  82. unlink( $this->getCachePath( $token ) );
  83. }
  84. /**
  85. * Delete all cache for current store.
  86. */
  87. private function _emptyAllCache() {
  88. array_map( 'unlink', glob( $this->storePath . "cache/*" ) );
  89. }
  90. }