StackExchange.Redis 使用LuaScript脚本模糊查询hash
原文:StackExchange.Redis 使用LuaScript脚本模糊查询hash
获取redis连接
public class RedisHelper
{
private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnection"].ConnectionString;
private static ConnectionMultiplexer _connection;
private static ConnectionMultiplexer Connection
{
get
{
if (_connection == null || !_connection.IsConnected)
{
_connection = ConnectionMultiplexer.Connect(ConnectionString);
}
return _connection;
}
} public static IDatabase GetDatabase()
{
ConnectionMultiplexer redis = Connection;
//return redis.GetDatabase(RedisDatebaseId);
return redis.GetDatabase();
}
}
//根据模糊查询条件获取key值集合
var pattern = "UserInfoHash:*";//匹配符
var redisResult = db.ScriptEvaluate(LuaScript.Prepare(
//Redis的keys模糊查询:
" local res = redis.call('KEYS', @keypattern) " +
" return res "), new { @keypattern = pattern });
string[] preSult = (string[])redisResult;//将返回的结果集转为数组
//根据key值集合获取所有Hash
var list = new List<ICustomInfo>();//定义存放客户信息的集合
string json = "{ 'UserInfoHash:0', 'UserInfoHash:1','UserInfoHash:2', 'UserInfoHash:4' }"; //Redis的keys模糊查询:
" local result={} local mykeys=" + json + "; " +
" for i,v in pairs(mykeys) do result[i]=redis.call('hgetall',v) end; " +
" return result"));
//将结果集转为数组
var vals = (StackExchange.Redis.RedisResult[])redisResult1;
foreach (var val in vals)
{
string[] preval = (string[])val;
int indexCount = Array.IndexOf(preval, "CustomerCode");
int indexPwd = Array.IndexOf(preval, "Password"); string ac = preval[indexCount + ];//账号的值,等于账号名称所在的下标+1
list.Add(new CustomInfo()
{
Password = preval[indexPwd + ],
Account = ac,
UpdateTime = DateTime.Now,
Token = StringExtension.ToBase64String(string.Format("{0}:{1}", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"), ac))
});
}
注:根据key模糊查询一次数据过多时,可能会报超时问题。 解决方法:在redis连接字符串加入对应的 syncTimeout=10000(发送/接收超时设置(毫秒) )
StackExchange.Redis 使用LuaScript脚本模糊查询hash的更多相关文章
- redis+lua实现脚本一键查询
场景 经常需要查redis某个key的值,需要执行三条命令才能查到 redis-cli,启动redis select num,选择db get key,查询语句 需要执行三条命令才能实现某个key的查 ...
- StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)
本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/ 目录 一.基础配置封装 二.String字符串类型数据操作封 ...
- StackExchange.Redis加载Lua脚本进行模糊查询的批量删除和修改
前言 使用StackExchange.Redis没有直接相关的方法进行模糊查询的批量删除和修改操作,虽然可以通过Scan相关的方法进行模糊查询,例如:HashScan("hashkey&qu ...
- stackExchange.redis 实现模糊匹配批量查询
如果使用redis的频次较高,那么业务中经常会出现需要根据关键字进行批量查询,所以总结一下StackExchange中使用批量查询的方法(如果数据量很大,那么在redis中模糊查询很耗时,请慎用!) ...
- StackExchange.Redis 模糊查询和删除
初始化连接对象 _connectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString" ...
- Redis中的关系查询(范围查询,模糊查询等...)
本文部分转自于:http://blog.csdn.net/dc_726/article/details/42784317 本文对Redis如何保存关系型数据,以及如何对其匹配.范围.模糊查询进行举例讲 ...
- StackExchange.Redis学习笔记(二) Redis查询 五种数据类型的应用
ConnectionMultiplexer ConnectionMultiplexer 是StackExchange.Redis的核心对象,用这个类的实例来进行Redis的一系列操作,对于一个整个应用 ...
- StackExchange.Redis 之 hash 类型示例
StackExchange.Redis 的组件封装示例网上有很多,自行百度搜索即可. 这里只演示如何使用Hash类型操作数据: // 在 hash 中存入或修改一个值 并设置order_hashkey ...
- Redis模糊查询
最近使用Redis优化项目功能,其中有一部分为模糊查询,找了很多帖子,也没有找到很好的解决方案和思路,最终皇天不负有心人啊,终于让我找到了!!! 感谢该帖作者:WalkerAlone 原文链接:ht ...
随机推荐
- Linux 内核层和 用户层 配置 GPIO 引脚
Linux BSP 开发的基础就是和GPIO打交道, 下面总结下这几天对某家开发板的GPIO控制的知识. 公司的开发板用的是 DTB 模式 ,首先,进入 dts,dtsi文件查看关于GPIO 的模块 ...
- 【容器化】容器技术实践.pdf_视频学习笔记
容器运行时 docker rkt gvisor containerd 容器编排系统:kubernetes (简称k8s)
- bzoj1706 [usaco2007 Nov]relays 奶牛接力跑 矩阵快速幂
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1706 题解 换个方法定义矩阵乘法:先加再取 \(\min\). 对于一个 \(n\times ...
- 网络安全意识有多重要?SamSam勒索软件敲诈了近600万美元
近年来,对于网络犯罪分子来说,勒索软件已成为数百万美元的黑市业务,SamSam就是一个很好的例子. 中国信息安全新研究显示,自2015年12月以来,SamSam勒索软件从受害者手中敲诈了近600万美元 ...
- 1N4148
摘自http://baike.baidu.com/link?url=0iTO7zZvHpCeJiZurTPpjDT95YdJu7cKdTeCWfol36b4JG5ii15leQ7K4wJWAZIBNb ...
- layer-框架使用修改部分
关于框架中js调子方法出错的处理 top.layer.open({ id: options.id, type: 2, shade: options.shade, title: options.titl ...
- 封装 pyinstaller -F -i b.ico excel.py
安装pywin32,可以参考<怎么给python安装pywin32模块?> 一定要注意对应的python版本,否则不能安装. 5怎么给python安装pywin32模块? 2 用命 ...
- vim安装bundle和使用
一.准备工作 安装Git(因为下面我们选择的插件管理器需要使用到它)安装其他插件前首先需要选择一个Vim插件管理器,我这里选择的是Vundle,Vundle的工作过程中需要通过Git自动从远程创库同步 ...
- BZOJ 2226: [Spoj 5971] LCMSum 莫比乌斯反演 + 严重卡常
Code: #pragma GCC optimize(2) #include<bits/stdc++.h> #define setIO(s) freopen(s".in" ...
- socket函数库简单封装
#pragma once #ifndef WINSOCK_H #include<WinSock2.h> #pragma comment(lib,"ws2_32.lib" ...