Skip to content

Commit 13f2dde

Browse files
committed
chore: Run CS Fixer
1 parent 697155f commit 13f2dde

File tree

4 files changed

+265
-255
lines changed

4 files changed

+265
-255
lines changed

webfiori/framework/cache/Cache.php

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,106 @@
1515
*/
1616
class Cache {
1717
/**
18-
*
18+
*
1919
* @var Storage
2020
*/
2121
private $driver;
2222
private static $inst;
2323
/**
24-
* Creates and returns a single instance of the class.
25-
*
26-
* @return Cache
24+
* Removes an item from the cache given its unique identifier.
25+
*
26+
* @param string $key
2727
*/
28-
private static function getInst() : Cache {
29-
if (self::$inst === null) {
30-
self::$inst = new Cache();
31-
self::setDriver(new FileStorage());
32-
}
33-
return self::$inst;
28+
public static function delete(string $key) {
29+
self::getDriver()->delete($key);
3430
}
3531
/**
36-
* Sets storage engine which is used to store, read, update and delete items
37-
* from the cache.
38-
*
39-
* @param Storage $driver
32+
* Removes all items from the cache.
4033
*/
41-
public static function setDriver(Storage $driver) {
42-
self::getInst()->driver = $driver;
34+
public static function flush() {
35+
self::getDriver()->flush();
36+
}
37+
/**
38+
* Returns or creates a cache item given its key.
39+
*
40+
*
41+
* @param string $key The unique identifier of the item.
42+
*
43+
* @param callable $generator A callback which is used as a fallback to
44+
* create new cache entry or re-create an existing one if it was expired.
45+
* This callback must return the data that will be cached.
46+
*
47+
* @param int $ttl Time to live of the item in seconds.
48+
*
49+
* @param array $params Any additional parameters to be passed to the callback
50+
* which is used to generate cache data.
51+
* @return null
52+
*/
53+
public static function get(string $key, callable $generator = null, int $ttl = 60, array $params = []) {
54+
$data = self::getDriver()->read($key);
55+
56+
if ($data !== null && $data !== false) {
57+
return $data;
58+
}
59+
60+
if (!is_callable($generator)) {
61+
return null;
62+
}
63+
$newData = call_user_func_array($generator, $params);
64+
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
65+
self::getDriver()->cache($item);
66+
67+
return $newData;
4368
}
4469
/**
4570
* Returns storage engine which is used to store, read, update and delete items
4671
* from the cache.
47-
*
72+
*
4873
* @return Storage
4974
*/
5075
public static function getDriver() : Storage {
5176
return self::getInst()->driver;
5277
}
5378
/**
54-
* Removes an item from the cache given its unique identifier.
55-
*
56-
* @param string $key
79+
* Reads an item from the cache and return its information.
80+
*
81+
* @param string $key The unique identifier of the item.
82+
*
83+
* @return Item|null If such item exist and not yet expired, an object
84+
* of type 'Item' is returned which has all cached item information. Other
85+
* than that, null is returned.
5786
*/
58-
public static function delete(string $key) {
59-
self::getDriver()->delete($key);
87+
public static function getItem(string $key) {
88+
return self::getDriver()->readItem($key);
6089
}
6190
/**
6291
* Checks if the cache has in item given its unique identifier.
63-
*
92+
*
6493
* @param string $key
65-
*
94+
*
6695
* @return bool If the item exist and is not yet expired, true is returned.
6796
* Other than that, false is returned.
6897
*/
6998
public static function has(string $key) : bool {
7099
return self::getDriver()->has($key);
71100
}
72-
/**
73-
* Removes all items from the cache.
74-
*/
75-
public static function flush() {
76-
self::getDriver()->flush();
77-
}
78101
/**
79102
* Creates new item in the cache.
80-
*
103+
*
81104
* Note that the item will only be added if it does not exist or already
82105
* expired or the override option is set to true in case it was already
83106
* created and not expired.
84-
*
107+
*
85108
* @param string $key The unique identifier of the item.
86-
*
109+
*
87110
* @param mixed $data The data that will be cached.
88-
*
111+
*
89112
* @param int $ttl The time at which the data will be kept in the cache (in seconds).
90-
*
113+
*
91114
* @param bool $override If cache item already exist which has given key and not yet
92115
* expired and this one is set to true, the existing item will be overridden by
93116
* provided data and ttl.
94-
*
117+
*
95118
* @return bool If successfully added, the method will return true. False
96119
* otherwise.
97120
*/
@@ -100,68 +123,50 @@ public static function set(string $key, $data, int $ttl = 60, bool $override = f
100123
$item = new Item($key, $data, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
101124
self::getDriver()->cache($item);
102125
}
126+
103127
return false;
104128
}
105129
/**
106-
* Reads an item from the cache and return its information.
107-
*
108-
* @param string $key The unique identifier of the item.
109-
*
110-
* @return Item|null If such item exist and not yet expired, an object
111-
* of type 'Item' is returned which has all cached item information. Other
112-
* than that, null is returned.
130+
* Sets storage engine which is used to store, read, update and delete items
131+
* from the cache.
132+
*
133+
* @param Storage $driver
113134
*/
114-
public static function getItem(string $key) {
115-
return self::getDriver()->readItem($key);
135+
public static function setDriver(Storage $driver) {
136+
self::getInst()->driver = $driver;
116137
}
117138
/**
118139
* Updates TTL of specific cache item.
119-
*
140+
*
120141
* @param string $key The unique identifier of the item.
121-
*
142+
*
122143
* @param int $ttl The new value for TTL.
123-
*
144+
*
124145
* @return bool If item is updated, true is returned. Other than that, false
125146
* is returned.
126147
*/
127148
public static function setTTL(string $key, int $ttl) {
128149
$item = self::getItem($key);
150+
129151
if ($item === null) {
130152
return false;
131153
}
132154
$item->setTTL($ttl);
133155
self::getDriver()->cache($item);
156+
134157
return true;
135158
}
136159
/**
137-
* Returns or creates a cache item given its key.
138-
*
139-
*
140-
* @param string $key The unique identifier of the item.
141-
*
142-
* @param callable $generator A callback which is used as a fallback to
143-
* create new cache entry or re-create an existing one if it was expired.
144-
* This callback must return the data that will be cached.
145-
*
146-
* @param int $ttl Time to live of the item in seconds.
147-
*
148-
* @param array $params Any additional parameters to be passed to the callback
149-
* which is used to generate cache data.
150-
* @return null
160+
* Creates and returns a single instance of the class.
161+
*
162+
* @return Cache
151163
*/
152-
public static function get(string $key, callable $generator = null, int $ttl = 60, array $params = []) {
153-
$data = self::getDriver()->read($key);
154-
155-
if ($data !== null && $data !== false) {
156-
return $data;
164+
private static function getInst() : Cache {
165+
if (self::$inst === null) {
166+
self::$inst = new Cache();
167+
self::setDriver(new FileStorage());
157168
}
158169

159-
if (!is_callable($generator)) {
160-
return null;
161-
}
162-
$newData = call_user_func_array($generator, $params);
163-
$item = new Item($key, $newData, $ttl, defined('CACHE_SECRET') ? CACHE_SECRET : '');
164-
self::getDriver()->cache($item);
165-
return $newData;
170+
return self::$inst;
166171
}
167172
}

0 commit comments

Comments
 (0)