Skip to content

Commit 568de86

Browse files
committed
Merge pull request #6 from mhh1422/master
Add TTL to mapping and document
2 parents 2cf4b5a + d4e5b02 commit 568de86

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ 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"
26+
"type" => "session",
27+
'ttl' => '15m' //check https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-ttl-field.html#_default_ttl
2728
],
2829
```
2930
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:
4243
"created":{"type":"date"},
4344
"updated":{"type":"date"},
4445
"data":{"type":"string","index":"no"}
46+
},
47+
"_ttl":{
48+
"enabled":true,
49+
"default":"15m"
4550
}
4651
}
4752
}

src/ElasticSessionStore.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static function putMapping() {
4444
'created' => ['type' => 'date'],
4545
'updated' => ['type' => 'date'],
4646
'data' => ['type' => 'string', 'index' => 'no'],
47-
]
47+
],
48+
'_ttl' => (@$config['ttl'] ? ['enabled' => true, 'default' => @$config['ttl']? : '15m'] : ['enabled' => false])
4849
]
4950
];
5051
$client->indices()->putMapping($mappingParams);
@@ -74,7 +75,7 @@ public function read($sessionId) {
7475
public function write($sessionId, $sessionData) {
7576
$updatedTs = Carbon::now()->toIso8601String();
7677
$createdTs = array_key_exists($sessionId, $this->_cache) ? $this->_cache[$sessionId]->created : $updatedTs;
77-
static::create(['data' => $sessionData, 'created' => $createdTs, 'updated' => $updatedTs], $sessionId, ["api" => "index"]);
78+
static::create(['data' => $sessionData, 'created' => $createdTs, 'updated' => $updatedTs, '_ttl' => config('session.elastic.ttl', '15m')], $sessionId, ["api" => "index"]);
7879
}
7980

8081
public function destroy($sessionId) {

0 commit comments

Comments
 (0)