目前使用的是 thinkphp,所以基本引入类库的方法使用的是 tp 的引入方法,其他引入方式一样
php 类库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| <?php
namespace lib;
use Redis;
class RedisPage {
protected $_redis;
protected $_redis_ip;
protected $_redis_port;
protected $_redis_db;
protected $_hash_prefix;
protected $_pre_dir;
public function __construct($ip = '', $port = 6379, $db = 2, $hash_prefix = 'toushi', $pre_dir = 'test') { $this->_redis_ip = $ip ? $ip : '127.0.0.1'; $this->_redis_port = $port ? $port : 6379; $this->_redis_db = $db ? $db : 0; $this->_hash_prefix = $hash_prefix ? $hash_prefix : ''; $this->_redis = new Redis(); $this->_redis->connect($this->_redis_ip, $this->_redis_port); $this->_redis->select($this->_redis_db);
$this->_pre_dir = $pre_dir ? $pre_dir.':' : 'test:'; }
public function set_page_info($id, $data) { if ( ! is_numeric($id) || ! is_array($data)) { return false; } $hashName = $this->_hash_prefix.'_'.$id;
$this->_redis->hMSet($this->_pre_dir.$hashName, $data);
$this->_redis->zAdd($this->_pre_dir.$this->_hash_prefix.'_sort', $id, $id);
return true; }
public function setKeyData($key, $data) { $preFix = 'toushi'; $data = serialize($data); $res = $this->_redis->set($preFix.$key, $data);
return $res; }
public function getKeyData($key) { $preFix = 'toushi'; $res = $this->_redis->get($preFix.$key); $res = unserialize($res);
return $res; }
public function keyIsExist($key) { $preFix = 'toushi'; $res = $this->_redis->exists($preFix.$key);
return $res ? true : false; }
public function get_page_info($page, $pageSize, $key = []) { if ( ! is_numeric($page) || ! is_numeric($pageSize)) { return false; } $limit_s = ($page - 1) * $pageSize; $limit_e = ($limit_s + $pageSize) - 1; $range = $this->_redis->ZREVRANGE($this->_pre_dir.$this->_hash_prefix.'_sort', $limit_s, $limit_e); $count = $this->_redis->zCard($this->_pre_dir.$this->_hash_prefix.'_sort'); $pageCount = ceil($count / $pageSize);
$page_data = []; foreach ($range as $item) { if (count($key) > 0) { $page_data[] = $this->_redis->hMGet($this->_pre_dir.$this->_hash_prefix.'_'.$item, $key); } else { $page_data[] = $this->_redis->hGetAll($this->_pre_dir.$this->_hash_prefix.'_'.$item); } }
$return_data = [ 'data' => $page_data, 'page' => $page, 'pageSize' => $pageSize, 'total' => $count, 'pageCount' => $pageCount, ];
return $return_data;
}
public function del_page_info($id) { if ( ! is_array($id)) { return false; } foreach ($id as $val) { $hashName = $this->_hash_prefix.'_'.$val; $this->_redis->del($this->_pre_dir.$hashName); $this->_redis->zRem($this->_pre_dir.$this->_hash_prefix.'_sort'.$val); }
return true; }
public function clear_db($db = 0) { if ($db >= 0) { $this->_redis->flushDB(); } elseif ($db == -1) { $this->_redis->flushAll(); } else { return false; }
return true; } }
|
类库使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| use lib\RedisPage;
$TASK_NAME = 'rcjk'; $REDIS_DB = 1;
$redis_config = config('cache.stores.redis'); $redis_config_host = $redis_config['host'] ?? ''; $redis_config_port = $redis_config['port'] ?? '';
$redis = new RedisPage($redis_config_host, $redis_config_port, $REDIS_DB, $TASK_NAME, 'toushi_'.$TASK_NAME);
$key_name = "test_name"; $key = "这是测试存储数据";
$redis->setKeyData($key_name, $key);
$redis->getKeyData($key_name);
$key_id = 1000; $arr =[] ; $redis->set_page_info($key_id, $arr);
$redis->get_page_info(1, 100);
$redis->clear_db($REDIS_DB);
|
目前只是其简单的使用,其他使用方法函数还在研究中…