Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
MemCachedDriver.php
Go to the documentation of this file.
1<?php
2
4
7
23 private const CONTENT = 'content';
24 private const TAG = 'tag';
25 private const TIME = 'time';
26
30 public function __construct($root, $postfix = "", $cacheParams = [ ]) {
31 parent::__construct ( $root, $postfix );
32 $defaultParams = [ 'servers' => [ [ 'host' => '0.0.0.0','port' => 11211 ] ],'serializer' => \Memcached::SERIALIZER_PHP,'persistent' => true ];
33 $cacheParams = \array_merge ( $defaultParams, $cacheParams );
34 $this->cacheInstance = new \Memcached ( $cacheParams ['persistent'] ? \crc32 ( $root ) : null );
35 if (isset ( $cacheParams ['serializer'] )) {
36 $this->cacheInstance->setOption ( \Memcached::OPT_SERIALIZER, $cacheParams ['serializer'] );
37 }
38 if ($this->cacheInstance->isPristine ()) {
39 $this->addServers ( $cacheParams ['servers'] );
40 }
41 }
42
43 public function addServer($host, $port, $weight = null) {
44 $this->cacheInstance->addServer ( $host, $port, $weight );
45 $statuses = $this->cacheInstance->getStats ();
46 if (! isset ( $statuses ["$host:$port"] )) {
47 throw new CacheException ( "Connection to the server $host:$port failed!" );
48 }
49 }
50
51 public function addServers(array $servers) {
52 foreach ( $servers as $srv ) {
53 $this->addServer ( $srv ['host'] ?? '0.0.0.0', $srv ['port'] ?? 11211, $srv ['weight'] ?? null);
54 }
55 }
56
57 public function setSerializer($serializer) {
58 $this->cacheInstance->setOption ( \Memcached::OPT_SERIALIZER, $serializer );
59 }
60
67 public function exists($key) {
68 $k = $this->getRealKey ( $key );
69 $this->cacheInstance->get ( $k );
70 return \Memcached::RES_NOTFOUND !== $this->cacheInstance->getResultCode ();
71 }
72
73 public function store($key, $content, $tag = null) {
74 $this->cacheInstance->set ( $this->getRealKey ( $key ), [ self::CONTENT => $content,self::TAG => $tag,self::TIME => \time () ] );
75 }
76
77 protected function getRealKey($key) {
78 return \crc32 ( $key );
79 }
80
87 public function fetch($key) {
88 $entry = $this->cacheInstance->get ( $this->getRealKey ( $key ) );
89 return $entry [self::CONTENT] ?? false;
90 }
91
98 public function file_get_contents($key) {
99 return $this->cacheInstance->get ( $this->getRealKey ( $key ) ) [self::CONTENT];
100 }
101
108 public function getTimestamp($key) {
109 $key = $this->getRealKey ( $key );
110 return $this->cacheInstance->get ( $key ) [self::TIME];
111 }
112
113 public function remove($key) {
114 $key = $this->getRealKey ( $key );
115 $this->cacheInstance->delete ( $this->getRealKey ( $key ) );
116 }
117
118 public function clear() {
119 $this->cacheInstance->flush ();
120 }
121
122 public function getCacheFiles($type) {
123 $result = [ ];
124 $keys = $this->cacheInstance->getAllKeys ();
125
126 foreach ( $keys as $key ) {
127 $entry = $this->cacheInstance->get ( $key );
128 if ($entry [self::TAG] === $type) {
129 $result [] = new CacheFile ( \ucfirst ( $type ), $key, $entry [self::TIME], "", $key );
130 }
131 }
132 if (\count ( $result ) === 0)
133 $result [] = new CacheFile ( \ucfirst ( $type ), "", "", "" );
134 return $result;
135 }
136
137 public function clearCache($type) {
138 $keys = $this->cacheInstance->getAllKeys ();
139 foreach ( $keys as $key ) {
140 $entry = $this->cacheInstance->get ( $key );
141 if ($entry [self::TAG] === $type) {
142 $this->cacheInstance->delete ( $key );
143 }
144 }
145 }
146
147 public function getCacheInfo() {
148 return parent::getCacheInfo () . "<br>Driver name : <b>" . \Memcached::class . "</b>";
149 }
150
151 public function getEntryKey($key) {
152 return $this->getRealKey ( $key );
153 }
154}
This class is responsible for storing Arrays in PHP files.
This class is responsible for storing values with MemCached.
file_get_contents($key)
return data stored for the given key.
fetch($key)
Fetches data stored for the given key.
__construct($root, $postfix="", $cacheParams=[])
Initializes the cache-provider.
exists($key)
Check if annotation-data for the key has been stored.
addServer($host, $port, $weight=null)
store($key, $content, $tag=null)
Caches the given data with the given key.
getTimestamp($key)
Returns the timestamp of the last cache update for the given key.