1
+ <?php
2
+
3
+ /**
4
+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com
5
+ * @author Georges.L (Geolim4) <[email protected] >
6
+ */
7
+
8
+ use phpFastCache \CacheManager ;
9
+ use phpFastCache \Helper \TestHelper ;
10
+
11
+ chdir (__DIR__ );
12
+ require_once __DIR__ . '/../vendor/autoload.php ' ;
13
+ $ testHelper = new TestHelper ('(P)Redis Expire TTL to 0 ' );
14
+ $ cacheInstance = CacheManager::getInstance ('Redis ' , []);
15
+ $ cacheKey = 'cacheKey ' ;
16
+ $ RandomCacheValue = str_shuffle (uniqid ('pfc ' , true ));
17
+ $ loops = 10 ;
18
+
19
+ $ testHelper ->printText ('See https://redis.io/commands/setex ' );
20
+ $ testHelper ->printText ('See https://redis.io/commands/expire ' );
21
+ $ testHelper ->printNewLine ();
22
+
23
+ for ($ i = 0 ; $ i <= $ loops ; $ i ++)
24
+ {
25
+ $ cacheItem = $ cacheInstance ->getItem ("{$ cacheKey }- {$ i }" );
26
+ $ cacheItem ->set ($ RandomCacheValue )
27
+ ->expiresAt (new DateTime ());
28
+
29
+ $ cacheInstance ->saveDeferred ($ cacheItem );
30
+ }
31
+
32
+ try {
33
+ $ cacheInstance ->commit ();
34
+ $ testHelper ->printPassText ('The COMMIT operation has finished successfully ' );
35
+ }catch (Predis \Response \ServerException $ e ){
36
+ if (strpos ($ e ->getMessage (), 'setex ' )){
37
+ $ testHelper ->printFailText ('The COMMIT operation has failed due to to an invalid time detection. ' );
38
+ }else {
39
+ $ testHelper ->printFailText ('The COMMIT operation has failed due to to an unexpected error: ' . $ e ->getMessage ());
40
+ }
41
+ }
42
+ $ cacheInstance ->detachAllItems ();
43
+
44
+ $ testHelper ->printText ('Sleeping a second... ' );
45
+
46
+
47
+ sleep (1 );
48
+
49
+ for ($ i = 0 ; $ i <= $ loops ; $ i ++)
50
+ {
51
+ $ cacheItem = $ cacheInstance ->getItem ("{$ cacheKey }- {$ i }" );
52
+
53
+ if ($ cacheItem ->isHit ()){
54
+ $ testHelper ->printFailText (sprintf ('The cache item "%s" is considered as HIT with the following value: %s ' , $ cacheItem ->getKey (), $ cacheItem ->get ()));
55
+ }else {
56
+ $ testHelper ->printPassText (sprintf ('The cache item "%s" is not considered as HIT. ' , $ cacheItem ->getKey ()));
57
+ }
58
+ }
59
+
60
+ $ cacheInstance ->clear ();
61
+
62
+ $ testHelper ->terminateTest ();
0 commit comments