From d4e5b02758e6d27ab372bfc145800fa825c7011c Mon Sep 17 00:00:00 2001 From: Muhannad Shelleh Date: Mon, 21 Mar 2016 17:07:31 +0400 Subject: [PATCH] Add TTL to mapping and document --- README.md | 7 ++++++- src/ElasticSessionStore.php | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c0bc924..861e69e 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ An elastic-search based session driver for Laravel 5.1 "elastic" => [ "url" => "http://localhost:9200/", "index" => "laravel-es-sessions", - "type" => "session" + "type" => "session", + 'ttl' => '15m' //check https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-ttl-field.html#_default_ttl ], ``` Values shown above are the default values in case you did not configure. @@ -42,6 +43,10 @@ You can do so manually by applying this mapping to the index and type: "created":{"type":"date"}, "updated":{"type":"date"}, "data":{"type":"string","index":"no"} + }, + "_ttl":{ + "enabled":true, + "default":"15m" } } } diff --git a/src/ElasticSessionStore.php b/src/ElasticSessionStore.php index aff47cd..6ca5c14 100644 --- a/src/ElasticSessionStore.php +++ b/src/ElasticSessionStore.php @@ -44,7 +44,8 @@ public static function putMapping() { 'created' => ['type' => 'date'], 'updated' => ['type' => 'date'], 'data' => ['type' => 'string', 'index' => 'no'], - ] + ], + '_ttl' => (@$config['ttl'] ? ['enabled' => true, 'default' => @$config['ttl']? : '15m'] : ['enabled' => false]) ] ]; $client->indices()->putMapping($mappingParams); @@ -74,7 +75,7 @@ public function read($sessionId) { public function write($sessionId, $sessionData) { $updatedTs = Carbon::now()->toIso8601String(); $createdTs = array_key_exists($sessionId, $this->_cache) ? $this->_cache[$sessionId]->created : $updatedTs; - static::create(['data' => $sessionData, 'created' => $createdTs, 'updated' => $updatedTs], $sessionId, ["api" => "index"]); + static::create(['data' => $sessionData, 'created' => $createdTs, 'updated' => $updatedTs, '_ttl' => config('session.elastic.ttl', '15m')], $sessionId, ["api" => "index"]); } public function destroy($sessionId) {