11<?php
2- namespace DominionEnterprises \Memoize ;
2+
3+ namespace TraderInteractive \Memoize ;
34
45use \Predis \Client ;
56
67/**
7- * A memoizer that caches the results in a redis cache.
8+ * A memoizer that caches the results in a redis cache.
89 */
910class Predis implements Memoize
1011{
@@ -13,37 +14,38 @@ class Predis implements Memoize
1314 *
1415 * @var \Predis\Client
1516 */
16- private $ _client ;
17+ private $ client ;
1718
1819 /**
1920 * Cache refresh
2021 *
2122 * @var boolean
2223 */
23- private $ _refresh ;
24+ private $ refresh ;
2425
2526 /**
2627 * Sets the predis client.
2728 *
28- * @param \Predis\Client $client The predis client to use
29- * @param boolean $refresh If true we will always overwrite cache even if it is already set
29+ * @param \Predis\Client $client The predis client to use
30+ * @param boolean $refresh If true we will always overwrite cache even if it is already set
3031 */
31- public function __construct (Client $ client , $ refresh = false )
32+ public function __construct (Client $ client , bool $ refresh = false )
3233 {
33- $ this ->_client = $ client ;
34- $ this ->_refresh = $ refresh ;
34+ $ this ->client = $ client ;
35+ $ this ->refresh = $ refresh ;
3536 }
3637
3738 /**
38- * The value is stored in redis as a json_encoded string, so make sure that the value you return from $compute is json-encodable.
39+ * The value is stored in redis as a json_encoded string,
40+ * so make sure that the value you return from $compute is json-encode-able.
3941 *
4042 * @see Memoize::memoizeCallable
4143 */
4244 public function memoizeCallable ($ key , $ compute , $ cacheTime = null )
4345 {
44- if (!$ this ->_refresh ) {
46+ if (!$ this ->refresh ) {
4547 try {
46- $ cached = $ this ->_client ->get ($ key );
48+ $ cached = $ this ->client ->get ($ key );
4749 if ($ cached !== null ) {
4850 $ data = json_decode ($ cached , true );
4951 return $ data ['result ' ];
@@ -55,30 +57,33 @@ public function memoizeCallable($key, $compute, $cacheTime = null)
5557
5658 $ result = call_user_func ($ compute );
5759
58- $ this ->_cache ($ key , json_encode (['result ' => $ result ]), $ cacheTime );
60+ $ this ->cache ($ key , json_encode (['result ' => $ result ]), $ cacheTime );
5961
6062 return $ result ;
6163 }
6264
6365 /**
6466 * Caches the value into redis with errors suppressed.
6567 *
66- * @param string $key The key.
67- * @param string $value The value.
68- * @param int $cacheTime The optional cache time
68+ * @param string $key The key.
69+ * @param string $value The value.
70+ * @param int $cacheTime The optional cache time
71+ *
6972 * @return void
7073 */
71- private function _cache ( $ key , $ value , $ cacheTime = null )
74+ private function cache ( string $ key , string $ value , int $ cacheTime = null )
7275 {
7376 try {
74- $ this ->_client ->set ($ key , $ value );
77+ $ this ->client ->set ($ key , $ value );
7578
7679 if ($ cacheTime !== null ) {
77- $ this ->_client ->expire ($ key , $ cacheTime );
80+ $ this ->client ->expire ($ key , $ cacheTime );
7881 }
7982 } catch (\Exception $ e ) {
80- // We don't want exceptions in accessing the cache to break functionality. The cache should be as transparent as possible.
81- // If insight is needed into these exceptions, a better way would be by notifying an observer with the errors.
83+ // We don't want exceptions in accessing the cache to break functionality.
84+ // The cache should be as transparent as possible.
85+ // If insight is needed into these exceptions,
86+ // a better way would be by notifying an observer with the errors.
8287 }
8388 }
8489}
0 commit comments