Skip to content

Commit b24ec63

Browse files
committed
mr.wen 完全 redis driver 和 laravel cach driver 两个底层的封装
1 parent 612e67f commit b24ec63

File tree

3 files changed

+385
-0
lines changed

3 files changed

+385
-0
lines changed

Src/Cache/InterfaceCache.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace CjwRepository\Src\Cache;
4+
5+
/**
6+
* 缓存驱动结构
7+
* Interface InterfaceCache
8+
* @package CjwRepository\Src\Cache
9+
*/
10+
interface InterfaceCache
11+
{
12+
13+
/**
14+
* 获取 hash
15+
* @param $key
16+
* @return mixed
17+
*/
18+
public static function exists($key);
19+
20+
/**
21+
* 获取 hash
22+
* @param $key
23+
* @return mixed
24+
*/
25+
public static function delete($key);
26+
27+
/**
28+
* 设置 或 更新 hash
29+
* @param $key
30+
* @param $value
31+
* @param $expire
32+
* @return mixed
33+
*/
34+
public static function setHash($key, $value, $expire);
35+
36+
/**
37+
* 获取 hash
38+
* @param $key
39+
* @return mixed
40+
*/
41+
public static function getHash($key);
42+
43+
/**
44+
* 设置 string 索引
45+
* @param $key
46+
* @param $value
47+
* @param $expire
48+
* @return mixed
49+
*/
50+
public static function setString($key, $value, $expire);
51+
52+
/**
53+
* 获取 string 索引
54+
* @param $key
55+
* @return mixed
56+
*/
57+
public static function getString($key);
58+
59+
/**
60+
* 从头部 开始 设置 list 索引
61+
* @param $key
62+
* @param $value
63+
* @param $expire
64+
* @return mixed
65+
*/
66+
public static function setListFromLeft($key, $value, $expire);
67+
68+
/**
69+
* 从尾部 开始 设置 list 索引
70+
* @param $key
71+
* @param $value
72+
* @param $expire
73+
* @return mixed
74+
*/
75+
public static function setListFromRight($key, $value, $expire);
76+
77+
/**
78+
* 获取全部索引
79+
* @param $key
80+
* @return mixed
81+
*/
82+
public static function getList($key);
83+
84+
/**
85+
* 获取部分索引
86+
* @param $key
87+
* @param $offset
88+
* @param $pageNum
89+
* @return mixed
90+
*/
91+
public static function getListRange($key, $offset, $pageNum);
92+
}
93+
94+
/**
95+
*
96+
* notice :
97+
* 1. 目前缓存驱动一共有两种,第一种就是redis 驱动, 一种就是 laravel 自带的cache 驱动进行缓存;但是他们都有利弊。
98+
*
99+
*
100+
* redis 驱动利弊问题:
101+
* 1. 无法存储一个空的list ,这样导致,如果某个list 索引查询到是的数据是一个空的数组, 则无法对其进行存储。
102+
* 则每次查询这个数据的时候,都会去查询数据库,但是基本上不会蛮多,如果数据库建立好索引后,对其也不会造成多大影响。
103+
*
104+
*
105+
*
106+
* laravel 的cache 驱动 利弊问题:
107+
* 1. 每次在做list 查询时候,必须把整个list全部都给拉出来后,在做list做操作,这样对io 和内存都是一个巨大的消耗,
108+
* 相比于 redis 驱动 利弊来讲, 这个情况太严重。
109+
*
110+
*
111+
*
112+
*/

Src/Cache/LaravelCacheDriver.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace CjwRepository\Src\Cache;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
class LaravelCacheDriver implements InterfaceCache
8+
{
9+
/**
10+
* 判断是否存在
11+
* @param $key
12+
* @return bool
13+
*/
14+
public static function exists($key)
15+
{
16+
return Cache::has($key);
17+
}
18+
19+
20+
/**
21+
* 删除 redis 键
22+
* @param $key
23+
* @return bool
24+
*/
25+
public static function delete($key)
26+
{
27+
return Cache::forget($key);
28+
}
29+
30+
/**
31+
* 设置 或 更新 hash
32+
* @param $key
33+
* @param $value
34+
* @param $expire
35+
* @return bool
36+
*/
37+
public static function setHash($key, $value, $expire)
38+
{
39+
return Cache::put($key, $value, $expire);
40+
}
41+
42+
/**
43+
* 获取 hash
44+
* @param $key
45+
* @return mixed
46+
*/
47+
public static function getHash($key)
48+
{
49+
return Cache::get($key);
50+
}
51+
52+
53+
/**
54+
* 设置 string
55+
* @param $key
56+
* @param $value
57+
* @param $expire
58+
* @return bool
59+
*/
60+
public static function setString($key, $value, $expire)
61+
{
62+
return Cache::put($key, $value, $expire);
63+
}
64+
65+
/**
66+
* 获取 string
67+
* @param $key
68+
* @return mixed
69+
*/
70+
public static function getString($key)
71+
{
72+
return Cache::get($key);
73+
}
74+
75+
/**
76+
* 从头部 开始 设置 list 索引
77+
* @param $key
78+
* @param $value
79+
* @param $expire
80+
* @return bool
81+
*/
82+
public static function setListFromLeft($key, $value, $expire)
83+
{
84+
$tem = Cache::get($key);
85+
$tem = empty($tem) ? [] : $tem;
86+
87+
$value = is_array($value) ? $value : [$value];
88+
foreach ($value as $item)
89+
array_unshift($tem, $item);
90+
91+
return Cache::put($key, $tem, $expire);
92+
}
93+
94+
/**
95+
* 从尾部 开始 设置 list 索引
96+
* @param $key
97+
* @param $value
98+
* @param $expire
99+
* @return bool
100+
*/
101+
public static function setListFromRight($key, $value, $expire)
102+
{
103+
$tem = Cache::get($key);
104+
$tem = empty($tem) ? [] : $tem;
105+
106+
$value = is_array($value) ? $value : [$value];
107+
foreach ($value as $item)
108+
array_push($tem, $item);
109+
110+
return Cache::put($key, $tem, $expire);
111+
}
112+
113+
/**
114+
* 获取全部索引
115+
* @param $key
116+
* @return mixed
117+
*/
118+
public static function getList($key)
119+
{
120+
return Cache::get($key);
121+
}
122+
123+
/**
124+
* 获取部分索引
125+
* @param $key
126+
* @param $offset
127+
* @param $pageNum
128+
* @return mixed
129+
*/
130+
public static function getListRange($key, $offset, $pageNum)
131+
{
132+
$tem = Cache::get($key);
133+
$tem = empty($tem) ? [] : $tem;
134+
135+
return array_slice($tem, $offset, $pageNum);
136+
}
137+
}

Src/Cache/RedisCacheDriver.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace CjwRepository\Src\Cache;
4+
5+
use Illuminate\Support\Facades\Redis;
6+
7+
class RedisCacheDriver implements InterfaceCache
8+
{
9+
/**
10+
* 判断是否存在
11+
* @param $key
12+
* @return bool
13+
*/
14+
public static function exists($key)
15+
{
16+
return (bool)Redis::exists($key);
17+
}
18+
19+
20+
/**
21+
* 删除 redis 键
22+
* @param $key
23+
* @return bool
24+
*/
25+
public static function delete($key)
26+
{
27+
return (bool)Redis::del($key);
28+
}
29+
30+
/**
31+
* 设置 或 更新 hash
32+
* @param $key
33+
* @param $value
34+
* @param $expire
35+
* @return bool
36+
*/
37+
public static function setHash($key, $value, $expire)
38+
{
39+
$resultSet = Redis::hmset($key, $value);
40+
41+
$resultExpire = Redis::expire($key, $expire * 60);
42+
43+
return ((String)$resultSet == 'OK') && !empty($resultExpire);
44+
}
45+
46+
/**
47+
* 获取 hash
48+
* @param $key
49+
* @return mixed
50+
*/
51+
public static function getHash($key)
52+
{
53+
return Redis::hgetall($key);
54+
}
55+
56+
57+
/**
58+
* 设置 string
59+
* @param $key
60+
* @param $value
61+
* @param $expire
62+
* @return bool
63+
*/
64+
public static function setString($key, $value, $expire)
65+
{
66+
$resultSet = Redis::set($key, $value);
67+
68+
$resultExpire = Redis::expire($key, $expire * 60);
69+
70+
return ((String)$resultSet == 'OK') && !empty($resultExpire);
71+
}
72+
73+
/**
74+
* 获取 string
75+
* @param $key
76+
* @return mixed
77+
*/
78+
public static function getString($key)
79+
{
80+
return Redis::get($key);
81+
}
82+
83+
/**
84+
* 从头部 开始 设置 list 索引
85+
* @param $key
86+
* @param $value
87+
* @param $expire
88+
* @return bool
89+
*/
90+
public static function setListFromLeft($key, $value, $expire)
91+
{
92+
$resultSet = Redis::lpush($key, $value);
93+
94+
$resultExpire = Redis::expire($key, $expire * 60);
95+
96+
return (bool)$resultSet && !empty($resultExpire);
97+
}
98+
99+
/**
100+
* 从尾部 开始 设置 list 索引
101+
* @param $key
102+
* @param $value
103+
* @param $expire
104+
* @return bool
105+
*/
106+
public static function setListFromRight($key, $value, $expire)
107+
{
108+
$resultSet = Redis::rpush($key, $value);
109+
110+
$resultExpire = Redis::expire($key, $expire * 60);
111+
112+
return (bool)$resultSet && !empty($resultExpire);
113+
}
114+
115+
/**
116+
* 获取全部索引
117+
* @param $key
118+
* @return mixed
119+
*/
120+
public static function getList($key)
121+
{
122+
return Redis::lrange($key, 0, -1);
123+
}
124+
125+
/**
126+
* 获取部分索引
127+
* @param $key
128+
* @param $offset
129+
* @param $pageNum
130+
* @return mixed
131+
*/
132+
public static function getListRange($key, $offset, $pageNum)
133+
{
134+
return Redis::lrange($key, $offset, $offset + $pageNum);
135+
}
136+
}

0 commit comments

Comments
 (0)