Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DbCache.php
Go to the documentation of this file.
1<?php
2
7
8use Ubiquity\cache\system\ArrayCache;
10
20abstract class DbCache {
21 protected $cache;
26 protected $memoryCache;
27 protected $storeDeferred;
28 protected $toStore = [ ];
29 public static $active = false;
30
31 protected function getKey($tableName, $condition) {
32 return $tableName . \md5 ( $condition );
33 }
34
35 public function __construct($cacheSystem = ArrayCache::class, $config = [ ]) {
36 if (\is_string ( $cacheSystem )) {
37 $this->cache = new $cacheSystem ( CacheManager::getCacheSubDirectory ( 'queries' ), '.query' );
38 } else {
39 $this->cache = $cacheSystem;
40 }
41 $this->storeDeferred = $config ['deferred'] ?? false;
42 }
43
44 protected function getMemoryCache($key) {
45 if (isset ( $this->memoryCache [$key] )) {
46 return $this->memoryCache [$key];
47 }
48 if ($this->cache->exists ( $key )) {
49 return $this->memoryCache [$key] = $this->cache->fetch ( $key );
50 }
51 return false;
52 }
53
61 abstract public function store($tableName, $condition, $result);
62
70 public function fetch($tableName, $condition) {
71 $key = $this->getKey ( $tableName, $condition );
72 if (isset ( $this->memoryCache [$key] )) {
73 return $this->memoryCache [$key];
74 }
75 if ($this->cache->exists ( $key )) {
76 return $this->memoryCache [$key] = $this->cache->fetch ( $key );
77 }
78 return false;
79 }
80
88 abstract public function delete($tableName, $condition);
89
90 public function clear($matches = '') {
91 $this->cache->clear ( $matches );
92 }
93
94 public function remove($key) {
95 $this->cache->remove ( $key );
96 }
97
98 public function setActive($value = true) {
99 self::$active = $value;
100 }
101
102 public function storeDeferred() {
103 foreach ( $this->toStore as $k ) {
104 $this->cache->store ( $k, $this->memoryCache [$k] );
105 }
106 $this->toStore = [ ];
107 }
108}
Manager for caches (Router, Rest, models).
static getCacheSubDirectory(string $subDirectory)
Returns an absolute cache subdirectory.
Abstract class for database caching Ubiquity\cache\database$DbCache This class is part of Ubiquity.
Definition DbCache.php:20
getKey($tableName, $condition)
Definition DbCache.php:31
__construct($cacheSystem=ArrayCache::class, $config=[])
Definition DbCache.php:35
fetch($tableName, $condition)
Fetches data stored for the given condition in table.
Definition DbCache.php:70
store($tableName, $condition, $result)
Caches the given data with the given key (tableName+md5(condition)).
Database cache systems.
Definition DbCache.php:6