@@ -15,14 +15,23 @@ class Predis implements Memoize
1515 */
1616 private $ _client ;
1717
18+ /**
19+ * Cache refresh
20+ *
21+ * @var boolean
22+ */
23+ private $ _refresh ;
24+
1825 /**
1926 * Sets the predis client.
2027 *
2128 * @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
2230 */
23- public function __construct (Client $ client )
31+ public function __construct (Client $ client, $ refresh = false )
2432 {
2533 $ this ->_client = $ client ;
34+ $ this ->_refresh = $ refresh ;
2635 }
2736
2837 /**
@@ -32,14 +41,16 @@ public function __construct(Client $client)
3241 */
3342 public function memoizeCallable ($ key , $ compute , $ cacheTime = null )
3443 {
35- try {
36- $ cached = $ this ->_client ->get ($ key );
37- if ($ cached !== null ) {
38- $ data = json_decode ($ cached , true );
39- return $ data ['result ' ];
44+ if (!$ this ->_refresh ) {
45+ try {
46+ $ cached = $ this ->_client ->get ($ key );
47+ if ($ cached !== null ) {
48+ $ data = json_decode ($ cached , true );
49+ return $ data ['result ' ];
50+ }
51+ } catch (\Exception $ e ) {
52+ return call_user_func ($ compute );
4053 }
41- } catch (\Exception $ e ) {
42- return call_user_func ($ compute );
4354 }
4455
4556 $ result = call_user_func ($ compute );
0 commit comments