Skip to content

Commit d0740c8

Browse files
refactor: 重构中,新增缓存库
1 parent f86b6ff commit d0740c8

17 files changed

Lines changed: 738 additions & 73 deletions

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.3.0"
16+
"php": ">=5.3.0",
17+
"predis/predis": "^1.1"
1718
},
1819
"require-dev": {
1920
"php": ">=5.6",

src/Cache/Cache.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace Hillpy\PHPTools\Cache;
4+
5+
use Hillpy\PHPTools\Cache\Interfaces\CacheInterface;
6+
use Hillpy\PHPTools\Cache\Traits\FileTrait;
7+
use Hillpy\PHPTools\Cache\Traits\MemcachedTrait;
8+
use Hillpy\PHPTools\Cache\Traits\RedisTrait;
9+
use Hillpy\PHPTools\Common\Common;
10+
11+
class Cache implements CacheInterface
12+
{
13+
use FileTrait,
14+
MemcachedTrait,
15+
RedisTrait;
16+
17+
// Cache类实例
18+
private static $instance;
19+
20+
// 配置参数
21+
private $options = [
22+
'driver' => 'file',
23+
'prefix' => 'cache_',
24+
'key' => 'cache',
25+
'expire' => 3600,
26+
'file_base_path' => '',
27+
'file_path' => '/cache',
28+
'file_ext' => 'php',
29+
'host' => '127.0.0.1',
30+
'port' => '',
31+
];
32+
33+
/**
34+
* 允许使用的驱动类型
35+
*
36+
* @var array
37+
*/
38+
private $allowDriver = [
39+
'file',
40+
'redis',
41+
'memcached',
42+
];
43+
44+
/**
45+
* 驱动trait数组
46+
*
47+
* @var array
48+
*/
49+
private $driverTraitArr = [
50+
'file' => FileTrait::class,
51+
'redis' => RedisTrait::class,
52+
'memcached' => MemcachedTrait::class,
53+
];
54+
55+
// 驱动trait
56+
private $driver;
57+
58+
/**
59+
* 允许传入的文件扩展
60+
*
61+
* @var array
62+
*/
63+
private $allowFileExt = [
64+
'php',
65+
'json',
66+
];
67+
68+
/**
69+
* 默认缓存端口
70+
*
71+
* @var array
72+
*/
73+
private $defaultPort = [
74+
'redis' => 6379,
75+
'memcached' => 11211,
76+
];
77+
78+
public static function getInstance($options = [])
79+
{
80+
if (is_null(self::$instance) || 1) {
81+
self::$instance = new self($options);
82+
} else {
83+
self::$instance->init($options);
84+
}
85+
86+
return self::$instance;
87+
}
88+
89+
private function __construct($options = [])
90+
{
91+
$this->init($options);
92+
}
93+
94+
private function __clone()
95+
{
96+
}
97+
98+
private function init($options = [])
99+
{
100+
if (
101+
is_array($options) &&
102+
count($options) > 0
103+
) {
104+
$this->options = Common::updateArrayData($this->options, $options);
105+
}
106+
in_array($this->options['file_ext'], $this->allowFileExt) || $this->options['file_ext'] = 'php';
107+
in_array($this->options['driver'], $this->allowDriver) || $this->options['driver'] = 'file';
108+
$this->options['port'] || (isset($this->defaultPort[$this->options['driver']]) && $this->options['port'] = $this->defaultPort[$this->options['driver']]);
109+
110+
$this->driver = $this->driverTraitArr[$this->options['driver']];
111+
$this->driver::init($this->options);
112+
}
113+
114+
public function setOptions($options = [])
115+
{
116+
if (
117+
is_array($options) &&
118+
count($options) > 0
119+
) {
120+
$this->options = Common::updateArrayData($this->options, $options);
121+
}
122+
123+
return $this;
124+
}
125+
126+
public function getOptions()
127+
{
128+
return $this->options;
129+
}
130+
131+
public function set($key = '', $value = '', $expire = '')
132+
{
133+
return $this->driver::set($key, $value, $expire);
134+
}
135+
136+
public function get($key = '')
137+
{
138+
return $this->driver::get($key);
139+
}
140+
141+
public function delete($key = '')
142+
{
143+
return $this->driver::delete($key);
144+
}
145+
146+
public function clear()
147+
{
148+
return $this->driver::clear();
149+
}
150+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Hillpy\PHPTools\Cache\Interfaces;
4+
5+
interface CacheInterface
6+
{
7+
public function set($key, $value, $expire);
8+
public function get($key);
9+
public function delete($key);
10+
public function clear();
11+
}

src/Cache/Traits/CommonTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Hillpy\PHPTools\Cache\Traits;
4+
5+
use Hillpy\PHPTools\Common\Common;
6+
7+
trait CommonTrait
8+
{
9+
public static function generateKey($prefix, $secretKey, $key)
10+
{
11+
return $prefix . md5($secretKey . $key);
12+
}
13+
14+
public static function encryptValue($value, $secretKey)
15+
{
16+
return Common::encryptData($value, $secretKey);
17+
}
18+
19+
public static function decryptValue($value, $secretKey)
20+
{
21+
return Common::decryptData($value, $secretKey);
22+
}
23+
}

0 commit comments

Comments
 (0)