redis-cli -h host -p port -a password | 首次進入redis 進行綁定ip和端口號 | |
---|---|---|
del key | 刪除指定key | |
exists key | 檢查指定key是否存在 | 1存在 0不存在 |
expire key seconds | 為key 設置過期時間 | 時間到后 自動刪除指定key |
persist key | 移除指定key的過期時間 | |
pttl key | 以毫秒返回所剩過期時間 | |
ttl key | 以秒返回所剩過期時間 | |
rename key newkey | 修改key的名稱 | |
type key | 返回 key的存儲類型 | |
LPUSH names value [valus...] | 將多個值 插入 names的列表的頭部 | RPUSH |
LINDEX names 1 | 通過索引獲取列表中的元素 | |
LLEN names | 獲取names列表的長度 | |
LPOP names | 移出並獲取列表的第一個元素 | RPOP |
LSET names[key] 0[index] ppp[value] | 通過索引設置列表元素的值 | |
LTRIM names start stop | 對一個列表進行修剪(trim),就是說,讓列表只保留指定區間內的元素,不在指定區間之內的元素都將被刪除。 | |
RPOP names | 移除列表的最后一個元素,返回值為移除的元素。 | LPOP |
KEYS names | 獲取所有key (當前庫中) | |
LRANGE names 0 -1 | 獲取 list 列表中指定key的所有元素 | |
...... |
||
實例操作:
function SetRedis($pwd) { //實例化redis對象 $redis = new Redis(); //連接redis $redis->connect('localhost',6379); $key = md5($pwd); $data = $redis->get($key);//如果data有值,此時應該是一個json字符串 if(!$data){ try{ $pdo = new PDO('mysql:dbname=test;host=localhost','root','root'); }catch(PDOException $e){ die("pdo連接失敗:".$e->getMessage()); } $res = $pdo->prepare($sql); $res->execute(); $data = json_encode($res->fetchAll(2));//將從數據庫取到的數據轉化為json字符串(為了存儲到redis中) $redis->set($key,$data); } return json_decode($data);//返回數組格式的數據 } $pwd = md5('abc123'); var_dump(SetRedis($sql));