Skip to content

Commit 219f0b8

Browse files
committed
mr.wen 添加缓存的雏形
1 parent b24ec63 commit 219f0b8

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

Src/Production/TemplateProduction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ protected function productionStorePrimaryKeyMethod(&$databaseStoreTemplate, $dat
428428
public function {$methodName}(\$where)
429429
{
430430
// 1. 字段过滤
431-
if (!array_keys_exists(\$this->primary_key, \$where))
431+
if (!array_keys_exists([\$this->primary_key], \$where))
432432
return false;
433433
434434
// 2. 数据数据
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace CjwRepository\Src\RepositoryLibrary;
4+
5+
use CjwRepository\Src\Cache\LaravelCacheDriver;
6+
use CjwRepository\Src\Cache\RedisCacheDriver;
7+
8+
/**
9+
* Class RepositoryCache
10+
* @package CjwRepository\Src\RepositoryLibrary
11+
*/
12+
class RepositoryCache
13+
{
14+
protected static $driver;
15+
16+
/**
17+
* 选择对应的缓存驱动
18+
* 注意: 对应的缓存驱动必须继承 CjwRepository\Src\Cache\InterfaceCache.php 这个接口
19+
* @param $driverName
20+
*/
21+
protected static function selectCacheDriver($driverName)
22+
{
23+
switch ($driverName) {
24+
case 'redis' :
25+
self::$driver = RedisCacheDriver::class;
26+
break;
27+
28+
case 'laravel':
29+
self::$driver = LaravelCacheDriver::class;
30+
break;
31+
32+
default:
33+
self::$driver = null;
34+
}
35+
}
36+
37+
38+
public static function createAllIndex($index, $driver, $callback)
39+
{
40+
41+
}
42+
43+
public static function updateAllIndex($index, $driver, $callback)
44+
{
45+
46+
}
47+
48+
/**
49+
* 通过find 查询 string 类型索引 的缓存内容
50+
* @param $index
51+
* @param $driver
52+
* @param $callback
53+
* @return bool
54+
*/
55+
public static function findStringIndex($index, $driver, $callback)
56+
{
57+
// 1. 获取驱动
58+
self::selectCacheDriver($driver);
59+
$driver = self::$driver;
60+
61+
if (empty($driver)) return false;
62+
63+
// 2. 判断回调函数是否存在
64+
if (!empty($callback)) return $callback($index, $driver);
65+
66+
// 3. 判断是否存在对键
67+
if (empty($driver::exists($index))) return false;
68+
69+
// 4. 查询并且返回
70+
return $driver::getString($index);
71+
}
72+
73+
74+
/**
75+
* 通过find 查询 hash 类型索引 的缓存内容
76+
* @param $index
77+
* @param $driver
78+
* @param $callback
79+
* @return bool
80+
*/
81+
public function findHashIndex($index, $driver, $callback)
82+
{
83+
// 1. 获取驱动
84+
self::selectCacheDriver($driver);
85+
$driver = self::$driver;
86+
87+
if (empty($driver)) return false;
88+
89+
// 2. 判断回调函数是否存在
90+
if (!empty($callback)) return $callback($index, $driver);
91+
92+
// 3. 判断是否存在对键
93+
if (empty($driver::exists($index))) return false;
94+
95+
// 4. 查询并且返回
96+
return $driver::getHash($index);
97+
}
98+
99+
/**
100+
* 通过find 查询 list 类型索引 的 缓存内容
101+
* @param $index
102+
* @param $driver
103+
* @param $callback
104+
* @return bool
105+
*/
106+
public function getList($index, $driver, $callback)
107+
{
108+
// 1. 获取驱动
109+
self::selectCacheDriver($driver);
110+
$driver = self::$driver;
111+
112+
if (empty($driver)) return false;
113+
114+
// 2. 判断回调函数是否存在
115+
if (!empty($callback)) return $callback($index, $driver);
116+
117+
// 3. 判断是否存在对键
118+
if (empty($driver::exists($index))) return false;
119+
120+
// 4. 查询并且返回
121+
return $driver::getList($index);
122+
}
123+
124+
125+
public function pageList($index, $offset, $pageNum, $driver, $callback)
126+
{
127+
// 1. 获取驱动
128+
self::selectCacheDriver($driver);
129+
$driver = self::$driver;
130+
131+
if (empty($driver)) return false;
132+
133+
// 2. 判断回调函数是否存在
134+
if (!empty($callback)) return $callback($index, $offset, $pageNum, $driver);
135+
136+
// 3. 判断是否存在对键
137+
if (empty($driver::exists($index))) return false;
138+
139+
// 4. 查询并且返回
140+
return $driver::getListRange($index, $offset, $pageNum);
141+
}
142+
}

0 commit comments

Comments
 (0)