php-使用哈希对象缓存
来源:互联网 发布:sql增删改查面试题 编辑:程序博客网 时间:2024/05/29 18:14
设置哈希缓存
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $data = $driver->hGetAll('people'); var_dump($data);exit;
output
array(4) { ["name"]=> string(3) "hls" ["age"]=> string(2) "20" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }
获取哈希缓存对象的所有key
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $data = $driver->hKeys('people'); var_dump($data);exit;
output
array(4) { [0]=> string(4) "name" [1]=> string(3) "age" [2]=> string(6) "height" [3]=> string(5) "width" }
获取哈希缓存对象的长度(key的个数)
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $data = $driver->hLen('people'); var_dump($data);exit;
output
int(4)
获取哈希缓存对象某个属性进行递增
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $driver->hIncrBy('people','age',5); $data = $driver->hGetAll('people'); var_dump($data);exit;
使用hIncrBy如果递增不存在的key 会默认创建key
output
array(4) { ["name"]=> string(3) "hls" ["age"]=> string(2) "25" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }
获取哈希缓存对象指定的key
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $data = $driver->hGet('people','name'); var_dump($data);exit;
output
string(3) "hls"
获取哈希缓存对象指定的keys(数组返回)
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $data = $driver->hMGet('people',['name','age']); var_dump($data);exit;
output
array(2) { ["name"]=> string(3) "hls" ["age"]=> string(2) "20" }
不存在的key 返回的值为false
设置哈希缓存对象指定的key的value
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $driver->hSet('people','age',100); $data = $driver->hGetAll('people'); var_dump($data);exit;
output
array(6) { ["name"]=> string(3) "hls" ["age"]=> string(3) "100" ["height"]=> string(3) "180" ["width"]=> string(2) "55" ["age1"]=> string(2) "10" ["age2"]=> string(1) "5" }
判断哈希缓存对象指定的key是否存在
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $result = $driver->hExists('people','qwe'); var_dump($result);exit;
output
bool(false)
删除哈希对象指定的属性key
<?php error_reporting(E_ALL); ini_set('display_errors','On'); $driver = new Redis(); $driver->connect('127.0.0.1',6379); $driver->select(0); $driver->delete('people'); $driver->hMset('people',[ 'name' => 'hls', 'age' => 20, 'height' => 180, 'width' => 55, ]); $result = $driver->hDel('people','name'); $data = $driver->hGetAll('people'); var_dump($data);exit;
output
array(3) { ["age"]=> string(2) "20" ["height"]=> string(3) "180" ["width"]=> string(2) "55" }
阅读全文
0 0
- php-使用哈希对象缓存
- 使用APC缓存PHP
- php缓存使用
- php output_buffering 缓存使用
- php output_buffering 缓存使用
- 如何使用PHP缓存?
- php output_buffering 缓存使用
- 缓存空对象的使用
- Android使用文件缓存对象
- 使用php设置页面缓存
- PHP之XCache缓存使用
- PHP XCache缓存安装使用
- php-使用redis字符串缓存
- Hibernate缓存-使用Ehcache让实体对象集合对象缓存
- php之对象使用
- php使用对象
- Ehcache 整合Spring 使用对象缓存、页面缓存
- Ehcache 整合Spring 使用对象缓存、页面缓存
- low rank读书笔记
- Convert BST to Greater Tree
- TP事物-不可缺少的操作
- MySQL存储过程和函数详细定义
- mysql数据库字符处理函数
- php-使用哈希对象缓存
- mysql数据库转sqlite.db数据库
- 深入理解Servlet
- 手把手教你使用Systrace(一)
- JavaScript学习笔记_尾调用优化
- 关于微信公众号支付获取用户openId的方法
- LevelDB使用指南
- 零基础学图形学(11) 几何知识——球坐标和三角函数
- Java面向对象习题