|
@@ -0,0 +1,38 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App;
|
|
4
|
+
|
|
5
|
+use Illuminate\Database\Eloquent\Model;
|
|
6
|
+use Redis;
|
|
7
|
+
|
|
8
|
+class RedisModel extends Model
|
|
9
|
+{
|
|
10
|
+ public static $redis;
|
|
11
|
+ private static function _init(){
|
|
12
|
+ if( empty(self::$redis ) ){
|
|
13
|
+ self::$redis = new Redis();
|
|
14
|
+ self::$redis->connect(config('constants.REDIS_IP'),config('constants.REDIS_PORT'));//链接
|
|
15
|
+ self::$redis->auth(config('constants.REDIS_PASSWORD'));//密码
|
|
16
|
+ }
|
|
17
|
+ return self::$redis ;
|
|
18
|
+ }
|
|
19
|
+ public static function get( $key ){
|
|
20
|
+ $redis = self::_init();
|
|
21
|
+ return $redis->get($key);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public static function set( $key, $data){
|
|
25
|
+ $redis = self::_init();
|
|
26
|
+ return $redis->set( $key, $data);
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ public static function expire( $key, $time = 60 ){
|
|
30
|
+ $redis = self::_init();
|
|
31
|
+ return $redis->expire( $key, $time );
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ public static function setnx( $key, $data){
|
|
35
|
+ $redis = self::_init();
|
|
36
|
+ return $redis->setnx( $key, $data);
|
|
37
|
+ }
|
|
38
|
+}
|