|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: yunwuxin <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace think\session\driver; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
14
|
|
|
use think\contract\SessionHandlerInterface; |
|
15
|
|
|
use think\helper\Arr; |
|
16
|
|
|
|
|
17
|
|
|
class Cache implements SessionHandlerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var CacheInterface */ |
|
|
|
|
|
|
21
|
|
|
protected $handler; |
|
22
|
|
|
|
|
23
|
|
|
/** @var integer */ |
|
|
|
|
|
|
24
|
|
|
protected $expire; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
|
|
|
|
|
27
|
|
|
protected $prefix; |
|
28
|
|
|
|
|
29
|
1 |
|
public function __construct(\think\Cache $cache, array $config = []) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->handler = $cache->store(Arr::get($config, 'store')); |
|
32
|
1 |
|
$this->expire = Arr::get($config, 'expire', 1440); |
|
33
|
1 |
|
$this->prefix = Arr::get($config, 'prefix', ''); |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function read(string $sessionId): string |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
1 |
|
return (string) $this->handler->get($this->prefix . $sessionId); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function delete(string $sessionId): bool |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
1 |
|
return $this->handler->delete($this->prefix . $sessionId); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function write(string $sessionId, string $data): bool |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->handler->set($this->prefix . $sessionId, $data, $this->expire); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|