@@ -14,27 +14,27 @@ discard block |
||
14 | 14 | class Connect |
15 | 15 | { |
16 | 16 | /** |
17 | - * The default connection type. |
|
18 | - * Currently, it supports 'mysql', 'sqlite', and 'pgsql'. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
17 | + * The default connection type. |
|
18 | + * Currently, it supports 'mysql', 'sqlite', and 'pgsql'. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | 22 | public static string $connection = 'sqlite'; |
23 | 23 | |
24 | 24 | /** |
25 | - * Holds the last error encountered during connection attempts. |
|
26 | - * |
|
27 | - * @var PDOException|null |
|
28 | - */ |
|
25 | + * Holds the last error encountered during connection attempts. |
|
26 | + * |
|
27 | + * @var PDOException|null |
|
28 | + */ |
|
29 | 29 | private static ?PDOException $error = null; |
30 | 30 | |
31 | 31 | |
32 | 32 | /** |
33 | - * Creates a new PDO instance based on the specified database configuration. |
|
34 | - * |
|
35 | - * @param array|null $database |
|
36 | - * @return PDO|null |
|
37 | - */ |
|
33 | + * Creates a new PDO instance based on the specified database configuration. |
|
34 | + * |
|
35 | + * @param array|null $database |
|
36 | + * @return PDO|null |
|
37 | + */ |
|
38 | 38 | public static function getInstance(?array $database = null) |
39 | 39 | { |
40 | 40 | $pdo = ConnectionFactory::createConnection($database); |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
48 | - * Sets the connection type for the database. |
|
49 | - * |
|
50 | - * @param string $connection |
|
51 | - * @return void |
|
52 | - */ |
|
48 | + * Sets the connection type for the database. |
|
49 | + * |
|
50 | + * @param string $connection |
|
51 | + * @return void |
|
52 | + */ |
|
53 | 53 | public static function setConnection(string $connection) |
54 | 54 | { |
55 | 55 | $drivers = ['mysql', 'sqlite', 'pgsql']; |
@@ -60,20 +60,20 @@ discard block |
||
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
63 | - * Gets the current connection type. |
|
64 | - * |
|
65 | - * @return string |
|
66 | - */ |
|
63 | + * Gets the current connection type. |
|
64 | + * |
|
65 | + * @return string |
|
66 | + */ |
|
67 | 67 | public static function getConnection() |
68 | 68 | { |
69 | 69 | return self::$connection; |
70 | 70 | } |
71 | 71 | |
72 | 72 | /** |
73 | - * Returns the last error encountered during connection attempts.\ |
|
74 | - * |
|
75 | - * @return PDOException|null |
|
76 | - */ |
|
73 | + * Returns the last error encountered during connection attempts.\ |
|
74 | + * |
|
75 | + * @return PDOException|null |
|
76 | + */ |
|
77 | 77 | public static function getError() |
78 | 78 | { |
79 | 79 | return self::$error; |
@@ -84,13 +84,13 @@ discard block |
||
84 | 84 | * This class is designed to be used statically, so it cannot be instantiated. |
85 | 85 | * |
86 | 86 | * @return void |
87 | - */ |
|
87 | + */ |
|
88 | 88 | private function __construct() {} |
89 | 89 | |
90 | 90 | /** |
91 | - * Prevents cloning of the Connect instance. |
|
92 | - * |
|
93 | - * @return void |
|
94 | - */ |
|
91 | + * Prevents cloning of the Connect instance. |
|
92 | + * |
|
93 | + * @return void |
|
94 | + */ |
|
95 | 95 | private function __clone() {} |
96 | 96 | } |
@@ -15,11 +15,11 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | /** |
18 | - * Creates a new PDO instance based on the specified database configuration. |
|
19 | - * |
|
20 | - * @param array|null $database |
|
21 | - * @return PDO|null |
|
22 | - */ |
|
18 | + * Creates a new PDO instance based on the specified database configuration. |
|
19 | + * |
|
20 | + * @param array|null $database |
|
21 | + * @return PDO|null |
|
22 | + */ |
|
23 | 23 | public static function createConnection(?array $database = null) |
24 | 24 | { |
25 | 25 | $dbConf = $database ?? CACHEER_DATABASE_CONFIG[Connect::getConnection()]; |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
53 | 53 | * @return mixed Returns the cached data or null if not found |
54 | 54 | */ |
55 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600); |
|
55 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * Retrieves multiple cache items by their keys. |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
63 | 63 | * @return CacheDataFormatter Returns a formatter with the retrieved items |
64 | 64 | */ |
65 | - public function getMany(array $cacheKeys, string $namespace, string|int $ttl = 3600); |
|
65 | + public function getMany(array $cacheKeys, string $namespace, string | int $ttl = 3600); |
|
66 | 66 | |
67 | 67 | /** |
68 | 68 | * Checks if a cache item exists. |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * @param string|int $ttl Lifetime in seconds (default: 3600) |
83 | 83 | * @return bool True on success, false on failure |
84 | 84 | */ |
85 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600); |
|
85 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600); |
|
86 | 86 | |
87 | 87 | /** |
88 | 88 | * Stores multiple items in the cache. |
@@ -12,13 +12,13 @@ |
||
12 | 12 | class OptionBuilder |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * Creates a FileOptionBuilder instance for file-based cache options. |
|
17 | - * |
|
18 | - * @return FileOptionBuilder |
|
19 | - */ |
|
20 | - public static function forFile() |
|
21 | - { |
|
15 | + /** |
|
16 | + * Creates a FileOptionBuilder instance for file-based cache options. |
|
17 | + * |
|
18 | + * @return FileOptionBuilder |
|
19 | + */ |
|
20 | + public static function forFile() |
|
21 | + { |
|
22 | 22 | return new FileOptionBuilder(); |
23 | - } |
|
23 | + } |
|
24 | 24 | } |
@@ -14,42 +14,42 @@ |
||
14 | 14 | class RedisCacheManager |
15 | 15 | { |
16 | 16 | |
17 | - /** @var Predis\Client */ |
|
18 | - private static $redis; |
|
19 | - |
|
20 | - /** @param string $namespace */ |
|
21 | - private static $namespace; |
|
22 | - |
|
23 | - /** |
|
24 | - * Connects to the Redis server using the configuration defined in REDIS_CONNECTION_CONFIG. |
|
25 | - * |
|
26 | - * @return Client |
|
27 | - */ |
|
28 | - public static function connect() |
|
29 | - { |
|
17 | + /** @var Predis\Client */ |
|
18 | + private static $redis; |
|
19 | + |
|
20 | + /** @param string $namespace */ |
|
21 | + private static $namespace; |
|
22 | + |
|
23 | + /** |
|
24 | + * Connects to the Redis server using the configuration defined in REDIS_CONNECTION_CONFIG. |
|
25 | + * |
|
26 | + * @return Client |
|
27 | + */ |
|
28 | + public static function connect() |
|
29 | + { |
|
30 | 30 | Autoloader::register(); |
31 | 31 | self::$redis = new Client([ |
32 | - 'scheme' => 'tcp', |
|
33 | - 'host' => REDIS_CONNECTION_CONFIG['REDIS_HOST'], |
|
34 | - 'port' => REDIS_CONNECTION_CONFIG['REDIS_PORT'], |
|
35 | - 'password' => REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'], |
|
36 | - 'database' => 0 |
|
32 | + 'scheme' => 'tcp', |
|
33 | + 'host' => REDIS_CONNECTION_CONFIG['REDIS_HOST'], |
|
34 | + 'port' => REDIS_CONNECTION_CONFIG['REDIS_PORT'], |
|
35 | + 'password' => REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'], |
|
36 | + 'database' => 0 |
|
37 | 37 | ]); |
38 | 38 | self::auth(); |
39 | 39 | self::$namespace = REDIS_CONNECTION_CONFIG['REDIS_NAMESPACE'] ?? 'Cache'; |
40 | 40 | return self::$redis; |
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Authenticates the Redis connection if a password is provided in the configuration. |
|
45 | - * |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - private static function auth() |
|
49 | - { |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Authenticates the Redis connection if a password is provided in the configuration. |
|
45 | + * |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + private static function auth() |
|
49 | + { |
|
50 | 50 | if(is_string(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']) && REDIS_CONNECTION_CONFIG['REDIS_PASSWORD'] !== '') { |
51 | - self::$redis->auth(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']); |
|
51 | + self::$redis->auth(REDIS_CONNECTION_CONFIG['REDIS_PASSWORD']); |
|
52 | + } |
|
52 | 53 | } |
53 | - } |
|
54 | 54 | |
55 | 55 | } |
@@ -13,119 +13,119 @@ |
||
13 | 13 | */ |
14 | 14 | class FileOptionBuilder |
15 | 15 | { |
16 | - /** @param null|string $cacheDir */ |
|
17 | - private ?string $cacheDir = null; |
|
18 | - |
|
19 | - /** @param null|string $expirationTime */ |
|
20 | - private ?string $expirationTime = null; |
|
21 | - |
|
22 | - /** @param null|string $flushAfter */ |
|
23 | - private ?string $flushAfter = null; |
|
24 | - |
|
25 | - /** @param array $options */ |
|
26 | - private array $options = []; |
|
27 | - |
|
28 | - /** |
|
29 | - * Directory where cache files will be stored. |
|
30 | - * |
|
31 | - * @param string $cacheDir |
|
32 | - * @return $this |
|
33 | - */ |
|
34 | - public function dir(string $cacheDir) |
|
35 | - { |
|
16 | + /** @param null|string $cacheDir */ |
|
17 | + private ?string $cacheDir = null; |
|
18 | + |
|
19 | + /** @param null|string $expirationTime */ |
|
20 | + private ?string $expirationTime = null; |
|
21 | + |
|
22 | + /** @param null|string $flushAfter */ |
|
23 | + private ?string $flushAfter = null; |
|
24 | + |
|
25 | + /** @param array $options */ |
|
26 | + private array $options = []; |
|
27 | + |
|
28 | + /** |
|
29 | + * Directory where cache files will be stored. |
|
30 | + * |
|
31 | + * @param string $cacheDir |
|
32 | + * @return $this |
|
33 | + */ |
|
34 | + public function dir(string $cacheDir) |
|
35 | + { |
|
36 | 36 | $this->cacheDir = $cacheDir; |
37 | 37 | return $this; |
38 | - } |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Sets the expiration time for cache items. |
|
42 | - * @param ?string $expirationTime |
|
43 | - * @return $this|TimeBuilder |
|
44 | - */ |
|
45 | - public function expirationTime(?string $expirationTime = null) |
|
46 | - { |
|
40 | + /** |
|
41 | + * Sets the expiration time for cache items. |
|
42 | + * @param ?string $expirationTime |
|
43 | + * @return $this|TimeBuilder |
|
44 | + */ |
|
45 | + public function expirationTime(?string $expirationTime = null) |
|
46 | + { |
|
47 | 47 | |
48 | 48 | if (!is_null($expirationTime)) { |
49 | - $this->expirationTime = $expirationTime; |
|
50 | - return $this; |
|
49 | + $this->expirationTime = $expirationTime; |
|
50 | + return $this; |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | return new TimeBuilder(function ($formattedTime){ |
54 | - $this->expirationTime = $formattedTime; |
|
54 | + $this->expirationTime = $formattedTime; |
|
55 | 55 | }, $this); |
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Sets the flush time for cache items. |
|
60 | - * This is the time after which the cache will be flushed. |
|
61 | - * |
|
62 | - * @param ?string $flushAfter |
|
63 | - * @return $this|TimeBuilder |
|
64 | - */ |
|
65 | - public function flushAfter(?string $flushAfter = null) |
|
66 | - { |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Sets the flush time for cache items. |
|
60 | + * This is the time after which the cache will be flushed. |
|
61 | + * |
|
62 | + * @param ?string $flushAfter |
|
63 | + * @return $this|TimeBuilder |
|
64 | + */ |
|
65 | + public function flushAfter(?string $flushAfter = null) |
|
66 | + { |
|
67 | 67 | |
68 | 68 | if (!is_null($flushAfter)) { |
69 | - $this->flushAfter = mb_strtolower($flushAfter, 'UTF-8'); |
|
70 | - return $this; |
|
69 | + $this->flushAfter = mb_strtolower($flushAfter, 'UTF-8'); |
|
70 | + return $this; |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | return new TimeBuilder(function ($formattedTime){ |
74 | - $this->flushAfter = $formattedTime; |
|
74 | + $this->flushAfter = $formattedTime; |
|
75 | 75 | }, $this); |
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Builds the options array for file cache configuration. |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - public function build() |
|
83 | - { |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Builds the options array for file cache configuration. |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + public function build() |
|
83 | + { |
|
84 | 84 | return $this->validated(); |
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Validates the properties and returns an array of options. |
|
89 | - * It checks if each property is valid and not null, then adds it to the options |
|
90 | - * |
|
91 | - * @return array |
|
92 | - */ |
|
93 | - private function validated() |
|
94 | - { |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Validates the properties and returns an array of options. |
|
89 | + * It checks if each property is valid and not null, then adds it to the options |
|
90 | + * |
|
91 | + * @return array |
|
92 | + */ |
|
93 | + private function validated() |
|
94 | + { |
|
95 | 95 | foreach ($this->properties() as $key => $value) { |
96 | 96 | if ($this->isValidAndNotNull($value)) { |
97 | 97 | $this->options[$key] = $value; |
98 | 98 | } |
99 | 99 | } |
100 | 100 | return $this->options; |
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Checks if the provided data is valid and not null. |
|
105 | - * This is used to ensure that only valid options are included in the final configuration. |
|
106 | - * |
|
107 | - * @param mixed $data |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - private function isValidAndNotNull(mixed $data) |
|
111 | - { |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Checks if the provided data is valid and not null. |
|
105 | + * This is used to ensure that only valid options are included in the final configuration. |
|
106 | + * |
|
107 | + * @param mixed $data |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + private function isValidAndNotNull(mixed $data) |
|
111 | + { |
|
112 | 112 | return !empty($data) ? true : false; |
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the properties of the FileOptionBuilder instance. |
|
117 | - * This method is used to gather the current state of the instance's properties. |
|
118 | - * |
|
119 | - * @return array |
|
120 | - */ |
|
121 | - private function properties() |
|
122 | - { |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the properties of the FileOptionBuilder instance. |
|
117 | + * This method is used to gather the current state of the instance's properties. |
|
118 | + * |
|
119 | + * @return array |
|
120 | + */ |
|
121 | + private function properties() |
|
122 | + { |
|
123 | 123 | $properties = [ |
124 | - 'cacheDir' => $this->cacheDir, |
|
125 | - 'expirationTime' => $this->expirationTime, |
|
126 | - 'flushAfter' => $this->flushAfter |
|
124 | + 'cacheDir' => $this->cacheDir, |
|
125 | + 'expirationTime' => $this->expirationTime, |
|
126 | + 'flushAfter' => $this->flushAfter |
|
127 | 127 | ]; |
128 | 128 | |
129 | 129 | return $properties; |
130 | - } |
|
130 | + } |
|
131 | 131 | } |
@@ -13,341 +13,341 @@ discard block |
||
13 | 13 | class ArrayCacheStore implements CacheerInterface |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @param array $arrayStore |
|
18 | - */ |
|
19 | - private array $arrayStore = []; |
|
20 | - |
|
21 | - /** |
|
22 | - * @param boolean |
|
23 | - */ |
|
24 | - private bool $success = false; |
|
25 | - |
|
26 | - /** |
|
27 | - * @param string |
|
28 | - */ |
|
29 | - private string $message = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var CacheLogger |
|
33 | - */ |
|
34 | - private $logger = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * ArrayCacheStore constructor. |
|
38 | - * |
|
39 | - * @param string $logPath |
|
40 | - */ |
|
41 | - public function __construct(string $logPath) |
|
42 | - { |
|
16 | + /** |
|
17 | + * @param array $arrayStore |
|
18 | + */ |
|
19 | + private array $arrayStore = []; |
|
20 | + |
|
21 | + /** |
|
22 | + * @param boolean |
|
23 | + */ |
|
24 | + private bool $success = false; |
|
25 | + |
|
26 | + /** |
|
27 | + * @param string |
|
28 | + */ |
|
29 | + private string $message = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var CacheLogger |
|
33 | + */ |
|
34 | + private $logger = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * ArrayCacheStore constructor. |
|
38 | + * |
|
39 | + * @param string $logPath |
|
40 | + */ |
|
41 | + public function __construct(string $logPath) |
|
42 | + { |
|
43 | 43 | $this->logger = new CacheLogger($logPath); |
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Appends data to an existing cache item. |
|
48 | - * |
|
49 | - * @param string $cacheKey |
|
50 | - * @param mixed $cacheData |
|
51 | - * @param string $namespace |
|
52 | - * @return bool |
|
53 | - */ |
|
54 | - public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
|
55 | - { |
|
56 | - $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
57 | - |
|
58 | - if (!$this->has($cacheKey, $namespace)) { |
|
59 | - $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
60 | - $this->logger->debug("{$this->getMessage()} from array driver."); |
|
61 | - return false; |
|
62 | - } |
|
63 | - |
|
64 | - $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
65 | - $this->setMessage("Cache appended successfully", true); |
|
66 | - return true; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Builds a unique key for the array store. |
|
71 | - * |
|
72 | - * @param string $cacheKey |
|
73 | - * @param string $namespace |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - private function buildArrayKey(string $cacheKey, string $namespace = '') |
|
77 | - { |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Appends data to an existing cache item. |
|
48 | + * |
|
49 | + * @param string $cacheKey |
|
50 | + * @param mixed $cacheData |
|
51 | + * @param string $namespace |
|
52 | + * @return bool |
|
53 | + */ |
|
54 | + public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
|
55 | + { |
|
56 | + $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
|
57 | + |
|
58 | + if (!$this->has($cacheKey, $namespace)) { |
|
59 | + $this->setMessage("cacheData can't be appended, because doesn't exist or expired", false); |
|
60 | + $this->logger->debug("{$this->getMessage()} from array driver."); |
|
61 | + return false; |
|
62 | + } |
|
63 | + |
|
64 | + $this->arrayStore[$arrayStoreKey]['cacheData'] = serialize($cacheData); |
|
65 | + $this->setMessage("Cache appended successfully", true); |
|
66 | + return true; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Builds a unique key for the array store. |
|
71 | + * |
|
72 | + * @param string $cacheKey |
|
73 | + * @param string $namespace |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + private function buildArrayKey(string $cacheKey, string $namespace = '') |
|
77 | + { |
|
78 | 78 | return !empty($namespace) ? ($namespace . ':' . $cacheKey) : $cacheKey; |
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Clears a specific cache item. |
|
83 | - * |
|
84 | - * @param string $cacheKey |
|
85 | - * @param string $namespace |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - public function clearCache(string $cacheKey, string $namespace = '') |
|
89 | - { |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Clears a specific cache item. |
|
83 | + * |
|
84 | + * @param string $cacheKey |
|
85 | + * @param string $namespace |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + public function clearCache(string $cacheKey, string $namespace = '') |
|
89 | + { |
|
90 | 90 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
91 | 91 | unset($this->arrayStore[$arrayStoreKey]); |
92 | 92 | $this->setMessage("Cache cleared successfully", true); |
93 | 93 | $this->logger->debug("{$this->getMessage()} from array driver."); |
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Decrements a cache item by a specified amount. |
|
98 | - * |
|
99 | - * @param string $cacheKey |
|
100 | - * @param int $amount |
|
101 | - * @param string $namespace |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
105 | - { |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Decrements a cache item by a specified amount. |
|
98 | + * |
|
99 | + * @param string $cacheKey |
|
100 | + * @param int $amount |
|
101 | + * @param string $namespace |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
|
105 | + { |
|
106 | 106 | return $this->increment($cacheKey, ($amount * -1), $namespace); |
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Flushes all cache items. |
|
111 | - * |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - public function flushCache() |
|
115 | - { |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Flushes all cache items. |
|
111 | + * |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + public function flushCache() |
|
115 | + { |
|
116 | 116 | unset($this->arrayStore); |
117 | 117 | $this->arrayStore = []; |
118 | 118 | $this->setMessage("Cache flushed successfully", true); |
119 | 119 | $this->logger->debug("{$this->getMessage()} from array driver."); |
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Stores a cache item permanently. |
|
124 | - * |
|
125 | - * @param string $cacheKey |
|
126 | - * @param mixed $cacheData |
|
127 | - * @param string $namespace |
|
128 | - * @param int|string $ttl |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function forever(string $cacheKey, mixed $cacheData) |
|
132 | - { |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Stores a cache item permanently. |
|
124 | + * |
|
125 | + * @param string $cacheKey |
|
126 | + * @param mixed $cacheData |
|
127 | + * @param string $namespace |
|
128 | + * @param int|string $ttl |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function forever(string $cacheKey, mixed $cacheData) |
|
132 | + { |
|
133 | 133 | $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000); |
134 | 134 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Retrieves a single cache item. |
|
139 | - * |
|
140 | - * @param string $cacheKey |
|
141 | - * @param string $namespace |
|
142 | - * @param int|string $ttl |
|
143 | - * @return mixed |
|
144 | - */ |
|
145 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
146 | - { |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Retrieves a single cache item. |
|
139 | + * |
|
140 | + * @param string $cacheKey |
|
141 | + * @param string $namespace |
|
142 | + * @param int|string $ttl |
|
143 | + * @return mixed |
|
144 | + */ |
|
145 | + public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
146 | + { |
|
147 | 147 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
148 | 148 | |
149 | 149 | if (!$this->has($cacheKey, $namespace)) { |
150 | - $this->handleCacheNotFound(); |
|
151 | - return false; |
|
150 | + $this->handleCacheNotFound(); |
|
151 | + return false; |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | $cacheData = $this->arrayStore[$arrayStoreKey]; |
155 | 155 | if ($this->isExpired($cacheData)) { |
156 | - $this->handleCacheExpired($arrayStoreKey); |
|
157 | - return false; |
|
156 | + $this->handleCacheExpired($arrayStoreKey); |
|
157 | + return false; |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | $this->setMessage("Cache retrieved successfully", true); |
161 | 161 | $this->logger->debug("{$this->getMessage()} from array driver."); |
162 | 162 | return $this->serialize($cacheData['cacheData'], false); |
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Verify if the cache is expired. |
|
167 | - * |
|
168 | - * @param array $cacheData |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - private function isExpired(array $cacheData): bool |
|
172 | - { |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Verify if the cache is expired. |
|
167 | + * |
|
168 | + * @param array $cacheData |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + private function isExpired(array $cacheData): bool |
|
172 | + { |
|
173 | 173 | $expirationTime = $cacheData['expirationTime'] ?? 0; |
174 | 174 | $now = time(); |
175 | 175 | return $expirationTime !== 0 && $now >= $expirationTime; |
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Handles the case when cache data is not found. |
|
180 | - * |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - private function handleCacheNotFound() |
|
184 | - { |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Handles the case when cache data is not found. |
|
180 | + * |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + private function handleCacheNotFound() |
|
184 | + { |
|
185 | 185 | $this->setMessage("cacheData not found, does not exists or expired", false); |
186 | 186 | $this->logger->debug("{$this->getMessage()} from array driver."); |
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Handles the case when cache data has expired. |
|
191 | - * |
|
192 | - * @param string $arrayStoreKey |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - private function handleCacheExpired(string $arrayStoreKey) |
|
196 | - { |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Handles the case when cache data has expired. |
|
191 | + * |
|
192 | + * @param string $arrayStoreKey |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + private function handleCacheExpired(string $arrayStoreKey) |
|
196 | + { |
|
197 | 197 | $parts = explode(':', $arrayStoreKey, 2); |
198 | 198 | if (count($parts) === 2) { |
199 | - list($np, $key) = $parts; |
|
199 | + list($np, $key) = $parts; |
|
200 | 200 | } else { |
201 | - $np = ''; |
|
202 | - $key = $arrayStoreKey; |
|
201 | + $np = ''; |
|
202 | + $key = $arrayStoreKey; |
|
203 | 203 | } |
204 | 204 | $this->clearCache($key, $np); |
205 | 205 | $this->setMessage("cacheKey: {$key} has expired.", false); |
206 | 206 | $this->logger->debug("{$this->getMessage()} from array driver."); |
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Gets all items in a specific namespace. |
|
211 | - * |
|
212 | - * @param string $namespace |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - public function getAll(string $namespace = '') |
|
216 | - { |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Gets all items in a specific namespace. |
|
211 | + * |
|
212 | + * @param string $namespace |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + public function getAll(string $namespace = '') |
|
216 | + { |
|
217 | 217 | $results = []; |
218 | 218 | foreach ($this->arrayStore as $key => $data) { |
219 | - if (strpos($key, $namespace . ':') === 0 || empty($namespace)) { |
|
219 | + if (strpos($key, $namespace . ':') === 0 || empty($namespace)) { |
|
220 | 220 | $results[$key] = $this->serialize($data['cacheData'], false); |
221 | - } |
|
221 | + } |
|
222 | 222 | } |
223 | 223 | return $results; |
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Retrieves multiple cache items by their keys. |
|
228 | - * |
|
229 | - * @param array $cacheKeys |
|
230 | - * @param string $namespace |
|
231 | - * @param string|int $ttl |
|
232 | - * @return array |
|
233 | - */ |
|
234 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
235 | - { |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Retrieves multiple cache items by their keys. |
|
228 | + * |
|
229 | + * @param array $cacheKeys |
|
230 | + * @param string $namespace |
|
231 | + * @param string|int $ttl |
|
232 | + * @return array |
|
233 | + */ |
|
234 | + public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
235 | + { |
|
236 | 236 | $results = []; |
237 | 237 | foreach ($cacheKeys as $cacheKey) { |
238 | - $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
238 | + $results[$cacheKey] = $this->getCache($cacheKey, $namespace, $ttl); |
|
239 | 239 | } |
240 | 240 | return $results; |
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Checks if a cache item exists. |
|
245 | - * |
|
246 | - * @param string $cacheKey |
|
247 | - * @param string $namespace |
|
248 | - * @return bool |
|
249 | - */ |
|
250 | - public function has(string $cacheKey, string $namespace = '') |
|
251 | - { |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Checks if a cache item exists. |
|
245 | + * |
|
246 | + * @param string $cacheKey |
|
247 | + * @param string $namespace |
|
248 | + * @return bool |
|
249 | + */ |
|
250 | + public function has(string $cacheKey, string $namespace = '') |
|
251 | + { |
|
252 | 252 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
253 | 253 | return isset($this->arrayStore[$arrayStoreKey]) && time() < $this->arrayStore[$arrayStoreKey]['expirationTime']; |
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Increments a cache item by a specified amount. |
|
258 | - * |
|
259 | - * @param string $cacheKey |
|
260 | - * @param int $amount |
|
261 | - * @param string $namespace |
|
262 | - * @return bool |
|
263 | - */ |
|
264 | - public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
265 | - { |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Increments a cache item by a specified amount. |
|
258 | + * |
|
259 | + * @param string $cacheKey |
|
260 | + * @param int $amount |
|
261 | + * @param string $namespace |
|
262 | + * @return bool |
|
263 | + */ |
|
264 | + public function increment(string $cacheKey, int $amount = 1, string $namespace = '') |
|
265 | + { |
|
266 | 266 | $cacheData = $this->getCache($cacheKey, $namespace); |
267 | 267 | |
268 | 268 | if(!empty($cacheData) && is_numeric($cacheData)) { |
269 | - $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
270 | - $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
271 | - return true; |
|
269 | + $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
|
270 | + $this->setMessage($this->getMessage(), $this->isSuccess()); |
|
271 | + return true; |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | return false; |
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Checks if the operation was successful. |
|
279 | - * |
|
280 | - * @return boolean |
|
281 | - */ |
|
282 | - public function isSuccess() |
|
283 | - { |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Checks if the operation was successful. |
|
279 | + * |
|
280 | + * @return boolean |
|
281 | + */ |
|
282 | + public function isSuccess() |
|
283 | + { |
|
284 | 284 | return $this->success; |
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Gets the last message. |
|
289 | - * |
|
290 | - * @return string |
|
291 | - */ |
|
292 | - public function getMessage() |
|
293 | - { |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Gets the last message. |
|
289 | + * |
|
290 | + * @return string |
|
291 | + */ |
|
292 | + public function getMessage() |
|
293 | + { |
|
294 | 294 | return $this->message; |
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Stores an item in the cache with a specific TTL. |
|
299 | - * |
|
300 | - * @param string $cacheKey |
|
301 | - * @param mixed $cacheData |
|
302 | - * @param string $namespace |
|
303 | - * @param int|string $ttl |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
307 | - { |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Stores an item in the cache with a specific TTL. |
|
299 | + * |
|
300 | + * @param string $cacheKey |
|
301 | + * @param mixed $cacheData |
|
302 | + * @param string $namespace |
|
303 | + * @param int|string $ttl |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
307 | + { |
|
308 | 308 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
309 | 309 | |
310 | 310 | $this->arrayStore[$arrayStoreKey] = [ |
311 | - 'cacheData' => serialize($cacheData), |
|
312 | - 'expirationTime' => time() + $ttl |
|
311 | + 'cacheData' => serialize($cacheData), |
|
312 | + 'expirationTime' => time() + $ttl |
|
313 | 313 | ]; |
314 | 314 | |
315 | 315 | $this->setMessage("Cache stored successfully", true); |
316 | 316 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
317 | 317 | return true; |
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Stores multiple items in the cache in batches. |
|
322 | - * |
|
323 | - * @param array $items |
|
324 | - * @param string $namespace |
|
325 | - * @param int $batchSize |
|
326 | - * @return void |
|
327 | - */ |
|
328 | - public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
|
329 | - { |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Stores multiple items in the cache in batches. |
|
322 | + * |
|
323 | + * @param array $items |
|
324 | + * @param string $namespace |
|
325 | + * @param int $batchSize |
|
326 | + * @return void |
|
327 | + */ |
|
328 | + public function putMany(array $items, string $namespace = '', int $batchSize = 100) |
|
329 | + { |
|
330 | 330 | $chunks = array_chunk($items, $batchSize, true); |
331 | 331 | |
332 | 332 | foreach ($chunks as $chunk) { |
333 | - foreach ($chunk as $key => $data) { |
|
334 | - $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
333 | + foreach ($chunk as $key => $data) { |
|
334 | + $this->putCache($data['cacheKey'], $data['cacheData'], $namespace); |
|
335 | + } |
|
335 | 336 | } |
336 | - } |
|
337 | 337 | $this->setMessage("{$this->getMessage()}", $this->isSuccess()); |
338 | 338 | $this->logger->debug("{$this->getMessage()} from Array driver."); |
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Renews the expiration time of a cache item. |
|
343 | - * |
|
344 | - * @param string $cacheKey |
|
345 | - * @param string|int $ttl |
|
346 | - * @param string $namespace |
|
347 | - * @return void |
|
348 | - */ |
|
349 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
350 | - { |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Renews the expiration time of a cache item. |
|
343 | + * |
|
344 | + * @param string $cacheKey |
|
345 | + * @param string|int $ttl |
|
346 | + * @param string $namespace |
|
347 | + * @return void |
|
348 | + */ |
|
349 | + public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
350 | + { |
|
351 | 351 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
352 | 352 | |
353 | 353 | if (isset($this->arrayStore[$arrayStoreKey])) { |
@@ -355,31 +355,31 @@ discard block |
||
355 | 355 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
356 | 356 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
357 | 357 | $this->logger->debug("{$this->getMessage()} from array driver."); |
358 | - } |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Sets a message and its success status. |
|
363 | - * |
|
364 | - * @param string $message |
|
365 | - * @param boolean $success |
|
366 | - * @return void |
|
367 | - */ |
|
368 | - private function setMessage(string $message, bool $success) |
|
369 | - { |
|
358 | + } |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Sets a message and its success status. |
|
363 | + * |
|
364 | + * @param string $message |
|
365 | + * @param boolean $success |
|
366 | + * @return void |
|
367 | + */ |
|
368 | + private function setMessage(string $message, bool $success) |
|
369 | + { |
|
370 | 370 | $this->message = $message; |
371 | 371 | $this->success = $success; |
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Serializes or unserializes data based on the flag. |
|
376 | - * |
|
377 | - * @param mixed $data |
|
378 | - * @param bool $serialize |
|
379 | - * @return mixed |
|
380 | - */ |
|
381 | - private function serialize(mixed $data, bool $serialize = true) |
|
382 | - { |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Serializes or unserializes data based on the flag. |
|
376 | + * |
|
377 | + * @param mixed $data |
|
378 | + * @param bool $serialize |
|
379 | + * @return mixed |
|
380 | + */ |
|
381 | + private function serialize(mixed $data, bool $serialize = true) |
|
382 | + { |
|
383 | 383 | return $serialize ? serialize($data) : unserialize($data); |
384 | - } |
|
384 | + } |
|
385 | 385 | } |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | * @param int|string $ttl |
143 | 143 | * @return mixed |
144 | 144 | */ |
145 | - public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600) |
|
145 | + public function getCache(string $cacheKey, string $namespace = '', string | int $ttl = 3600) |
|
146 | 146 | { |
147 | 147 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
148 | 148 | |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | * @param string|int $ttl |
232 | 232 | * @return array |
233 | 233 | */ |
234 | - public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600) |
|
234 | + public function getMany(array $cacheKeys, string $namespace = '', string | int $ttl = 3600) |
|
235 | 235 | { |
236 | 236 | $results = []; |
237 | 237 | foreach ($cacheKeys as $cacheKey) { |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | { |
266 | 266 | $cacheData = $this->getCache($cacheKey, $namespace); |
267 | 267 | |
268 | - if(!empty($cacheData) && is_numeric($cacheData)) { |
|
268 | + if (!empty($cacheData) && is_numeric($cacheData)) { |
|
269 | 269 | $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace); |
270 | 270 | $this->setMessage($this->getMessage(), $this->isSuccess()); |
271 | 271 | return true; |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | * @param int|string $ttl |
304 | 304 | * @return bool |
305 | 305 | */ |
306 | - public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
|
306 | + public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int | string $ttl = 3600) |
|
307 | 307 | { |
308 | 308 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
309 | 309 | |
@@ -346,12 +346,12 @@ discard block |
||
346 | 346 | * @param string $namespace |
347 | 347 | * @return void |
348 | 348 | */ |
349 | - public function renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
|
349 | + public function renewCache(string $cacheKey, int | string $ttl = 3600, string $namespace = '') |
|
350 | 350 | { |
351 | 351 | $arrayStoreKey = $this->buildArrayKey($cacheKey, $namespace); |
352 | 352 | |
353 | 353 | if (isset($this->arrayStore[$arrayStoreKey])) { |
354 | - $ttlSeconds = is_numeric($ttl) ? (int) $ttl : strtotime($ttl) - time(); |
|
354 | + $ttlSeconds = is_numeric($ttl) ? (int)$ttl : strtotime($ttl) - time(); |
|
355 | 355 | $this->arrayStore[$arrayStoreKey]['expirationTime'] = time() + $ttlSeconds; |
356 | 356 | $this->setMessage("cacheKey: {$cacheKey} renewed successfully", true); |
357 | 357 | $this->logger->debug("{$this->getMessage()} from array driver."); |
@@ -46,13 +46,13 @@ |
||
46 | 46 | private string $lastFlushTimeFile; |
47 | 47 | |
48 | 48 | /** |
49 | - * @var CacheLogger |
|
50 | - */ |
|
49 | + * @var CacheLogger |
|
50 | + */ |
|
51 | 51 | private $logger = null; |
52 | 52 | |
53 | 53 | /** |
54 | - * @var FileCacheManager |
|
55 | - */ |
|
54 | + * @var FileCacheManager |
|
55 | + */ |
|
56 | 56 | private FileCacheManager $fileManager; |
57 | 57 | |
58 | 58 |
@@ -53,24 +53,24 @@ |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
56 | - * Converts the exception to a JSON serializable format. |
|
57 | - * |
|
58 | - * @return string |
|
59 | - */ |
|
56 | + * Converts the exception to a JSON serializable format. |
|
57 | + * |
|
58 | + * @return string |
|
59 | + */ |
|
60 | 60 | public function jsonSerialize(): array |
61 | 61 | { |
62 | 62 | return parent::jsonSerialize(); |
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
66 | - * Converts the exception to a JSON string. |
|
67 | - * |
|
68 | - * @param int $options |
|
69 | - * @return string |
|
70 | - */ |
|
66 | + * Converts the exception to a JSON string. |
|
67 | + * |
|
68 | + * @param int $options |
|
69 | + * @return string |
|
70 | + */ |
|
71 | 71 | public function toJson(int $options = 0) |
72 | 72 | { |
73 | - return parent::toJson($options); |
|
73 | + return parent::toJson($options); |
|
74 | 74 | } |
75 | 75 | } |
76 | 76 |