1
1
<?php
2
- /**
3
- * Created by PhpStorm.
4
- * User: arthurkushman
5
- * Date: 04.03.18
6
- * Time: 11:07
7
- */
8
2
9
3
namespace rjapi \extension ;
10
4
11
-
12
- use Predis \Client ;
13
- use rjapi \helpers \ConfigHelper ;
14
- use rjapi \types \ConfigInterface ;
5
+ use Illuminate \Support \Facades \Redis ;
15
6
use rjapi \types \PhpInterface ;
16
7
8
+ /**
9
+ * Trait CacheTrait
10
+ * @package rjapi\extension
11
+ */
17
12
trait CacheTrait
18
13
{
19
- /** @var Client $cache */
20
- private $ cache ;
21
- private $ host ;
22
- private $ port ;
23
-
24
- public function cacheConn () : void
25
- {
26
- $ this ->setHostAndPort ();
27
- $ this ->cache = new Client ([
28
- 'scheme ' => 'tcp ' ,
29
- 'host ' => $ this ->host ,
30
- 'port ' => $ this ->port ,
31
- ]);
32
- // select db
33
- $ db = ConfigHelper::getNestedParam (ConfigInterface::CACHE , ConfigInterface::DATABASE );
34
- if ($ db !== null ) {
35
- $ this ->cache ->select ($ db );
36
- }
37
- // enter password
38
- $ password = env ('REDIS_PASSWORD ' );
39
- if ($ password !== null ) {
40
- $ this ->cache ->auth ($ password );
41
- }
42
- }
43
-
44
- private function setHostAndPort () : void
45
- {
46
- $ cacheEntity = ConfigHelper::getNestedParam (ConfigInterface::CACHE , $ this ->entity );
47
- $ this ->host = empty ($ cacheEntity [ConfigInterface::HOST ]) ? null : $ cacheEntity [ConfigInterface::HOST ];
48
- if ($ this ->host === null ) {
49
- $ this ->host = env ('REDIS_HOST ' );
50
- if ($ this ->host === null ) {
51
- $ this ->host = ConfigInterface::DEFAULT_REDIS_HOST ;
52
- }
53
- }
54
-
55
- $ this ->port = empty ($ cacheEntity [ConfigInterface::PORT ]) ? null : $ cacheEntity [ConfigInterface::PORT ];
56
- if ($ this ->port === null ) {
57
- $ this ->port = env ('REDIS_PORT ' );
58
- if ($ this ->port === null ) {
59
- $ this ->port = ConfigInterface::DEFAULT_REDIS_PORT ;
60
- }
61
- }
62
- }
63
-
64
14
/**
65
15
* @param string $key
66
16
* @param \League\Fractal\Resource\Collection | \League\Fractal\Resource\Item $val
67
17
* @return mixed
68
18
*/
69
19
private function set (string $ key , $ val )
70
20
{
71
- return $ this -> cache -> set ($ key , $ this ->ser ($ val ));
21
+ return Redis:: set ($ key , $ this ->ser ($ val ));
72
22
}
73
23
74
24
/**
@@ -77,7 +27,7 @@ private function set(string $key, $val)
77
27
*/
78
28
private function get (string $ key )
79
29
{
80
- $ data = $ this -> cache -> get ($ key );
30
+ $ data = Redis:: get ($ key );
81
31
if ($ data === null ) {
82
32
return null ;
83
33
}
0 commit comments