@@ -35,6 +35,7 @@ public function __construct(array $sessionConfig) {
3535
3636 public static function putMapping () {
3737 $ config = config ('session.elastic ' , static ::$ defaultConfig );
38+ $ ttl = $ this ->getTTL ();
3839 $ client = new \Elasticsearch \Client (['hosts ' => [$ config ['url ' ]]]);
3940 $ mappingParams = [
4041 'index ' => $ config ['index ' ],
@@ -45,7 +46,7 @@ public static function putMapping() {
4546 'updated ' => ['type ' => 'date ' ],
4647 'data ' => ['type ' => 'string ' , 'index ' => 'no ' ],
4748 ],
48- '_ttl ' => (@ $ config [ ' ttl ' ] ? ['enabled ' => true , 'default ' => @ $ config [ ' ttl ' ] ? : '15m ' ] : ['enabled ' => false ])
49+ '_ttl ' => ($ ttl ? ['enabled ' => true , 'default ' => $ ttl ? : '30m ' ] : ['enabled ' => false ])
4950 ]
5051 ];
5152 $ client ->indices ()->putMapping ($ mappingParams );
@@ -75,7 +76,8 @@ public function read($sessionId) {
7576 public function write ($ sessionId , $ sessionData ) {
7677 $ updatedTs = Carbon::now ()->toIso8601String ();
7778 $ createdTs = array_key_exists ($ sessionId , $ this ->_cache ) ? $ this ->_cache [$ sessionId ]->created : $ updatedTs ;
78- static ::create (['data ' => $ sessionData , 'created ' => $ createdTs , 'updated ' => $ updatedTs , '_ttl ' => config ('session.elastic.ttl ' , '15m ' )], $ sessionId , ["api " => "index " ]);
79+ $ ttl = $ this ->getTTL ();
80+ static ::create (['data ' => $ sessionData , 'created ' => $ createdTs , 'updated ' => $ updatedTs ] + ($ ttl ? ['_ttl ' => $ ttl ] : []), $ sessionId , ["api " => "index " ]);
7981 }
8082
8183 public function destroy ($ sessionId ) {
@@ -94,4 +96,10 @@ public function type() {
9496 return $ this ->sessionConfig ['type ' ];
9597 }
9698
99+ protected function getTTL () {
100+ $ ttl = config ('session.lifetime ' , 30 );
101+ $ ttl .=$ ttl ? 'm ' : '' ;
102+ return !!$ ttl ? $ ttl : false ;
103+ }
104+
97105}
0 commit comments