thinkphp6 redis 哈希存储方式以及操作函数(笔记)

逻辑:如果redis里没有指定表数据就进行存储再输出,如果有就直接输出,代码优化后几万条数据从数据库入redis也是三四秒的时间,数据以json方式存储:key用于数据ID 跟数据库数据ID同步,value用于存储整个字段包括数据,这样数据多不会占用多余内存

$name参数是数据库表名,跟数据库表名是同步一样的,直接调用即可

php 复制代码
/**
 * 缓存指定表全部数据
 * @Author Xven <[email protected]>
 * @return [type]                  [description]
 */
function redis_data($name) {
	$redis = Cache::store('redis');
	$data = [];
	// 尝试从 Redis 获取数据
	$lists = $redis->hGetAll($name . ':list');
	if (!empty($lists)) {
		// Redis 中有数据,直接解码并返回
		foreach ($lists as $key => $value) {
			$data[$key] = json_decode($value, true);
		}
	} else {
		// Redis 中没有数据,从数据库获取并写入 Redis
		$list = Db::name($name)->cursor();
		$encodedData = [];
		// 开始一个多重操作
		$redis->multi();
		foreach ($list as $item) {
			$encoded = json_encode($item, JSON_UNESCAPED_UNICODE);
			$redis->hSet($name . ':list', $item['id'], $encoded);
			$data[] = $item; // 直接将数据库查询结果添加到 $data 数组中
		}
		// 执行多重操作中的所有命令
		$redis->exec();
	}

	return $data;
}
php 复制代码
/**
 * 查询指定ID单条数据
 * @Author Xven <[email protected]>
 * @param  [type]                  $name [description]
 * @param  [type]                  $id   [description]
 * @return [type]                        [description]
 */
function find_redis($name, $id) {
	$redis = Cache::store('redis');
	$info = $redis->hMget($name . ':list', [$id]);
	if (!empty($info)) {
		$info = json_decode($info[$id], true);
		return $info;
	} else {
		return '';
	}
}
php 复制代码
/**
 * 指定ID数据重存更新
 * @Author Xven <[email protected]>
 * @param  [type]                  $id   [description]
 * @param  [type]                  $name [description]
 * @return [type]                        [description]
 */
function update_redis($name, $id, $field) {
	$redis = Cache::store('redis');
	$cursor = Db::name($name)->where('id', $id)->limit(1)->cursor();
	foreach ($cursor as $v) {
		if (!empty($v)) {
			$sela = $redis->hSet($field, $id, json_encode($v));
			if ($sela) {
				return true;
			} else {
				return false;
			}
		}
	}
}
php 复制代码
/**
 * 指定ID数据重存更新
 * @Author Xven <[email protected]>
 * @param  [type]                  $id   [description]
 * @param  [type]                  $name [description]
 * @return [type]                        [description]
 */
function del_redis($field, $id) {
	$redis = Cache::store('redis');
	$info = $redis->hDel($field, $id);
	if ($info) {
		return true;
	} else {
		return false;
	}
}
相关推荐
技术宝哥7 小时前
Redis(2):Redis + Lua为什么可以实现原子性
数据库·redis·lua
dddaidai1239 小时前
Redis解析
数据库·redis·缓存
工一木子10 小时前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis
Chasing__Dreams12 小时前
Redis--基础知识点--26--过期删除策略 与 淘汰策略
数据库·redis·缓存
亚林瓜子13 小时前
Spring集成Redis中禁用主机名DNS检测
redis·spring·ssh
joker D88816 小时前
【C++】深入理解 unordered 容器、布隆过滤器与分布式一致性哈希
c++·分布式·哈希算法
jingyu飞鸟18 小时前
Centos7系统(最小化安装)安装zabbix7版本详细文章、nginx源代码配置、php源代码、mysql-yum安装
开发语言·php
Q_Q196328847519 小时前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php
小白学大数据19 小时前
基于Scrapy-Redis的分布式景点数据爬取与热力图生成
javascript·redis·分布式·scrapy
Kookoos20 小时前
Redis + ABP vNext 构建分布式高可用缓存架构
redis·分布式·缓存·架构·c#·.net