1111
1212namespace Cache \CacheBundle \Session ;
1313
14+ use Cache \Taggable \TaggablePoolInterface ;
1415use Psr \Cache \CacheItemPoolInterface ;
1516
1617/**
17- * Class SessionHandler
18+ * Class SessionHandler.
1819 *
1920 * @author Aaron Scherer <[email protected] > 2021 */
@@ -26,7 +27,7 @@ class SessionHandler implements \SessionHandlerInterface
2627 private $ cache ;
2728
2829 /**
29- * @var integer Time to live in seconds
30+ * @var int Time to live in seconds
3031 */
3132 private $ ttl ;
3233
@@ -43,40 +44,40 @@ class SessionHandler implements \SessionHandlerInterface
4344 * * expiretime: The time to live in seconds
4445 *
4546 * @param CacheItemPoolInterface $cache A Cache instance
46- * @param array $options An associative array of cache options
47+ * @param array $options An associative array of cache options
4748 *
4849 * @throws \InvalidArgumentException When unsupported options are passed
4950 */
5051 public function __construct (CacheItemPoolInterface $ cache , array $ options = array ())
5152 {
5253 $ this ->cache = $ cache ;
5354
54- $ this ->ttl = isset ($ options ['ttl ' ]) ? (int ) $ options ['ttl ' ] : 86400 ;
55+ $ this ->ttl = isset ($ options ['ttl ' ]) ? (int ) $ options ['ttl ' ] : 86400 ;
5556 $ this ->prefix = isset ($ options ['prefix ' ]) ? $ options ['prefix ' ] : 'sf2ses_ ' ;
5657 }
5758
5859 /**
59- * {@inheritDoc }
60+ * {@inheritdoc }
6061 */
6162 public function open ($ savePath , $ sessionName )
6263 {
6364 return true ;
6465 }
6566
6667 /**
67- * {@inheritDoc }
68+ * {@inheritdoc }
6869 */
6970 public function close ()
7071 {
7172 return true ;
7273 }
7374
7475 /**
75- * {@inheritDoc }
76+ * {@inheritdoc }
7677 */
7778 public function read ($ sessionId )
7879 {
79- $ item = $ this ->cache -> getItem ( $ this -> prefix . $ sessionId );
80+ $ item = $ this ->getCacheItem ( $ sessionId );
8081 if ($ item ->isHit ()) {
8182 return $ item ->get ();
8283 }
@@ -85,31 +86,49 @@ public function read($sessionId)
8586 }
8687
8788 /**
88- * {@inheritDoc }
89+ * {@inheritdoc }
8990 */
9091 public function write ($ sessionId , $ data )
9192 {
92- $ item = $ this ->cache -> getItem ( $ this -> prefix . $ sessionId );
93+ $ item = $ this ->getCacheItem ( $ sessionId );
9394 $ item ->set ($ data )
9495 ->expiresAfter ($ this ->ttl );
9596
9697 return $ this ->cache ->save ($ item );
9798 }
9899
99100 /**
100- * {@inheritDoc }
101+ * {@inheritdoc }
101102 */
102103 public function destroy ($ sessionId )
103104 {
104- return $ this ->cache ->deleteItem ($ this ->prefix . $ sessionId );
105+ if ($ this ->cache instanceof TaggablePoolInterface) {
106+ return $ this ->cache ->deleteItem ($ this ->prefix .$ sessionId , ['session ' ]);
107+ }
108+
109+ return $ this ->cache ->deleteItem ($ this ->prefix .$ sessionId );
105110 }
106111
107112 /**
108- * {@inheritDoc }
113+ * {@inheritdoc }
109114 */
110115 public function gc ($ lifetime )
111116 {
112117 // not required here because cache will auto expire the records anyhow.
113118 return true ;
114119 }
120+
121+ /**
122+ * @param $sessionId
123+ *
124+ * @return \Psr\Cache\CacheItemInterface
125+ */
126+ private function getCacheItem ($ sessionId )
127+ {
128+ if ($ this ->cache instanceof TaggablePoolInterface) {
129+ return $ this ->cache ->getItem ($ this ->prefix .$ sessionId , ['session ' ]);
130+ }
131+
132+ return $ this ->cache ->getItem ($ this ->prefix .$ sessionId );
133+ }
115134}
0 commit comments