.htaccess
1 year ago
CacheHandler.php
1 year ago
ConditionsHandler.php
1 year ago
DocumentFinder.php
1 year ago
DocumentReducer.php
1 year ago
DocumentUpdater.php
4 months ago
IoHelper.php
4 months ago
NestedHelper.php
1 year ago
index.html
1 year ago
web.config
1 year ago
CacheHandler.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace SleekDB\Classes; |
| 5 | |
| 6 | |
| 7 | use SleekDB\Cache; |
| 8 | use SleekDB\Exceptions\IOException; |
| 9 | use SleekDB\QueryBuilder; |
| 10 | |
| 11 | /** |
| 12 | * Class CacheHandler |
| 13 | * Bridge between Query and Cache |
| 14 | */ |
| 15 | class CacheHandler |
| 16 | { |
| 17 | /** |
| 18 | * @var Cache |
| 19 | */ |
| 20 | protected $cache; |
| 21 | |
| 22 | protected $cacheTokenArray; |
| 23 | protected $regenerateCache; |
| 24 | protected $useCache; |
| 25 | |
| 26 | /** |
| 27 | * CacheHandler constructor. |
| 28 | * @param string $storePath |
| 29 | * @param QueryBuilder $queryBuilder |
| 30 | */ |
| 31 | public function __construct(string $storePath, QueryBuilder $queryBuilder) |
| 32 | { |
| 33 | $this->cacheTokenArray = $queryBuilder->_getCacheTokenArray(); |
| 34 | |
| 35 | $queryBuilderProperties = $queryBuilder->_getConditionProperties(); |
| 36 | $this->useCache = $queryBuilderProperties["useCache"]; |
| 37 | $this->regenerateCache = $queryBuilderProperties["regenerateCache"]; |
| 38 | |
| 39 | $this->cache = new Cache($storePath, $this->_getCacheTokenArray(), $queryBuilderProperties["cacheLifetime"]); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @return Cache |
| 44 | */ |
| 45 | public function getCache(): Cache |
| 46 | { |
| 47 | return $this->cache; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get results from cache |
| 52 | * @return array|null |
| 53 | * @throws IOException |
| 54 | */ |
| 55 | public function getCacheContent($getOneDocument) |
| 56 | { |
| 57 | if($this->getUseCache() !== true){ |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | $this->updateCacheTokenArray(['oneDocument' => $getOneDocument]); |
| 62 | |
| 63 | if($this->regenerateCache === true) { |
| 64 | $this->getCache()->delete(); |
| 65 | } |
| 66 | |
| 67 | $cacheResults = $this->getCache()->get(); |
| 68 | if(is_array($cacheResults)) { |
| 69 | return $cacheResults; |
| 70 | } |
| 71 | |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add content to cache |
| 77 | * @param array $results |
| 78 | * @throws IOException |
| 79 | */ |
| 80 | public function setCacheContent(array $results) |
| 81 | { |
| 82 | if($this->getUseCache() === true){ |
| 83 | $this->getCache()->set($results); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Delete all cache files that have no lifetime. |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function deleteAllWithNoLifetime(): bool |
| 92 | { |
| 93 | return $this->getCache()->deleteAllWithNoLifetime(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a reference to the array used for cache token generation |
| 98 | * @return array |
| 99 | */ |
| 100 | public function &_getCacheTokenArray(): array |
| 101 | { |
| 102 | return $this->cacheTokenArray; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param array $tokenUpdate |
| 107 | */ |
| 108 | private function updateCacheTokenArray(array $tokenUpdate) |
| 109 | { |
| 110 | if(empty($tokenUpdate)) { |
| 111 | return; |
| 112 | } |
| 113 | $cacheTokenArray = $this->_getCacheTokenArray(); |
| 114 | foreach ($tokenUpdate as $key => $value){ |
| 115 | $cacheTokenArray[$key] = $value; |
| 116 | } |
| 117 | $this->cacheTokenArray = $cacheTokenArray; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Status if cache is used or not |
| 122 | * @return bool |
| 123 | */ |
| 124 | private function getUseCache(): bool |
| 125 | { |
| 126 | return $this->useCache; |
| 127 | } |
| 128 | |
| 129 | } |