Skip to content

Commit ce16ae3

Browse files
committed
Merge pull request #7 from mhh1422/master
Add TTL to es documents
2 parents 568de86 + 926b4be commit ce16ae3

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ An elastic-search based session driver for Laravel 5.1
2323
"elastic" => [
2424
"url" => "http://localhost:9200/",
2525
"index" => "laravel-es-sessions",
26-
"type" => "session",
27-
'ttl' => '15m' //check https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-ttl-field.html#_default_ttl
26+
"type" => "session"
2827
],
28+
"lifetime" => 30
2929
```
30-
Values shown above are the default values in case you did not configure.
30+
Values shown above for elastic parameter are the default values in case you did not configure.
3131
3232
##Index/Type mapping
3333
Elastic will detect the mapping by default, however, it is recommended to set the mapping explicitly.
@@ -46,7 +46,7 @@ You can do so manually by applying this mapping to the index and type:
4646
},
4747
"_ttl":{
4848
"enabled":true,
49-
"default":"15m"
49+
"default":"30m"
5050
}
5151
}
5252
}
@@ -58,6 +58,9 @@ Or simpler, the package can do it for you. You will need to tinker `./artisan ti
5858
\ItvisionSy\LaravelElasticSessionDriver\ElasticSessionStore::putMapping();
5959
```
6060

61+
Please note that the `putMapping()` method will automatically read the values from your sessions config file
62+
including the `session.lifetime` value (in minutes) which will be used as the default TTL value.
63+
6164
## Author
6265
Muhannad Shelleh <[email protected]>
6366

src/ElasticSessionStore.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)