[转]Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
转自:http://www.cnblogs.com/oppoic/p/6165581.html
Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法有存用户令牌、短信验证码等
官网显示Redis本身并没有Windows版本的,微软官方开发了基于Windows的Redis服务器:MSOpenTech/redis
一、Redis服务端
首先下载Redis服务器,点击前往下载.msi版本,双击安装Redis服务端就有了,并以服务的形式随系统一起启动:
安装好Redis服务器之后第一件事就是设置密码,进入安装目录:C:\Program Files\Redis - 找到配置文件:redis.windows-service.conf - 找到:# requirepass foobared - 回车换行加上:requirepass 这里写自己的新密码(顶行写,前面不要留空格) - 到服务里重启Redis服务,或者重启电脑
不设置密码的坏处,看看携程这哥们的遭遇就知道了:记一次Redis被攻击的事件
二、Redis客户端(命令行和可视化工具RDM)
命令行方式演示:启动Redis客户端、读写Redis服务器
上图命令解释:
cd C:\Program Files\Redis:cd命令进入Redis安装目录,相当于Windows系统里双击进入Redis的安装目录
redis-cli.exe:打开redis-cli客户端程序,相当于Windows系统里双击运行一个exe程序(安装了上面的Redis服务端程序,需要一个客户端程序连接这个服务端。连接本机redis服务器直接敲此命令,连接远程的需要加ip和端口,例:redis-cli.exe -h 111.11.11.111 -p 6379)
keys *:查看所有键值对(如果Redis服务器设置了密码,这条命令会报错,需要先输入密码,执行此命令:auth 你的密码)
set blog oppoic.cnblogs.com:设置一个键值对,键是:blog,值是:oppoic.cnblogs.com(按目录存储:set 目录名:键 值)
get blog:获取键为blog对应的值
keys *:查看所有键值对
其他常用命令:
config get dir:获取redis安装目录
ping:返回PONG表示redis服务器正常
redis-cli.exe:进入第一个数据库(默认),redis一共0到15共16个库,进入第三个库 redis-cli -n 2(已经进去了,select 0~15 随意切换)
quit:退出redis程序
exit:退出dos窗口
flushdb:删除当前选择数据库中的所有key
flushall:删除所有数据库中的数据库
更多命令:https://redis.io/commands
至此,一个运行在本机的Redis缓存服务器已经搭建完成,并且可以读写了。但是命令行显然对小白用户不友好,可视化工具登场:Redis Desktop Manager
左侧树显示已经有一个连接了,点击底部的Connect to Redis Server再添加一个连接:
Name:连接名称,随便起
Host:主机地址,本机就是127.0.0.1,远程的输入对应IP
Port:端口,Redis服务器默认端口6379
Auth:密码,设置了就输,没设置留空
连上Redis服务器就可以看到,默认16个库(配置文件可改),索引从0开始。常见用法是一个项目一个库,项目下不同功能模块分不同目录存在这个库下。
有了可视化工具之后的操作就不用说了,双击,右键新建、删除。。。会用Windows系统的都会用这个工具。相比于命令行,Redis Desktop Manager这个可视化工具更友好,调试远程服务器上的数据也更方便,指哪打哪。
注:本机可以这样,连接远程服务器需要到服务器上的Redis安装目录下,找到redis.windows-service.conf文件,找到bind 127.0.0.1 前面加"#"注释掉,然后到服务里右键重启redis服务
三、C#操作Redis服务器
以上都是命令行和可视化工具操作Redis服务器,C#程序操作Redis需要借助StackExchange.Redis,为了统一调用,封装了一个RedisHelper帮助类:
2017.02.08:RedisHelper.cs代码有修改,请下载文章结尾提供的源码
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Text;
using System.Linq; namespace redis_Demo
{
/// <summary>
/// Redis 帮助类
/// </summary>
public static class RedisHelper
{
private static string _conn = ConfigurationManager.AppSettings["redis_connection_string"] ?? "127.0.0.1:6379";
private static string _pwd = ConfigurationManager.AppSettings["redis_connection_pwd"] ?? "";
private static int _store_db = string.IsNullOrEmpty(ConfigurationManager.AppSettings["redis_store_db"]) ? - : Convert.ToInt32(ConfigurationManager.AppSettings["redis_store_db"]); static ConnectionMultiplexer _redis;
static readonly object _locker = new object(); #region 单例模式
public static ConnectionMultiplexer Manager
{
get
{
if (_redis == null)
{
lock (_locker)
{
if (_redis != null) return _redis;
_redis = GetManager();
return _redis;
}
}
return _redis;
}
} private static ConnectionMultiplexer GetManager(string connectionString = null)
{
if (string.IsNullOrEmpty(connectionString))
{
connectionString = _conn;
}
var options = ConfigurationOptions.Parse(connectionString);
options.Password = _pwd;
return ConnectionMultiplexer.Connect(options);
}
#endregion #region 辅助方法
/// <summary>
/// 获取要操作的库
/// </summary>
/// <param name="db">库,0和-1都是第一个库,1是第二个库...</param>
/// <returns></returns>
private static int GetOperationDB(int db)
{
if (db == -)
{
return _store_db;
}
else
{
return db;
}
} private static string ConvertJson<T>(T value)
{
string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
return result;
} private static T ConvertObj<T>(RedisValue value)
{
if (string.IsNullOrEmpty(value))
{
return default(T);
}
else
{
return JsonConvert.DeserializeObject<T>(value);
}
} private static List<T> ConvetList<T>(RedisValue[] values)
{
List<T> result = new List<T>();
foreach (var item in values)
{
var model = ConvertObj<T>(item);
if (model != null)
result.Add(model);
}
return result;
} private static RedisKey[] ConvertRedisKeys(List<string> redisKeys, string prefix)
{
if (string.IsNullOrEmpty(prefix))
{
return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
}
else
{
return redisKeys.Select(redisKey => (RedisKey)(prefix + ":" + redisKey)).ToArray();
}
} /// <summary>
/// 获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
private static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
} FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstead == true)
{
return name;
}
return attribute == null ? null : attribute.Description;
}
#endregion #region 通用(key)
/// <summary>
/// 是否存在
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
public static bool KeyExists(CacheFolderEnum folder, string key, int db = -)
{
try
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).KeyExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 设置过期时间
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="min">过期时间,单位:分钟</param>
/// <param name="db">库,默认读取配置文件</param>
public static bool KeyExpire(CacheFolderEnum folder, string key, int min = , int db = -)
{
try
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).KeyExpire(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, DateTime.Now.AddMinutes(min));
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 修改键
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="newKey">新键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool KeyRename(CacheFolderEnum folder, string key, string newKey, int db = -)
{
try
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).KeyRename(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, string.IsNullOrEmpty(fd) ? newKey : fd + ":" + newKey);
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 删除
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool KeyDelete(CacheFolderEnum folder, string key, int db = -)
{
try
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).KeyDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
}
catch (Exception)
{
return false;
}
} /// <summary>
/// 批量删除
/// </summary>
/// <param name="folder">目录</param>
/// <param name="keys">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long KeyDelete(CacheFolderEnum folder, List<string> keys, int db = -)
{
try
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).KeyDelete(ConvertRedisKeys(keys, fd));
}
catch (Exception)
{
return ;
}
}
#endregion #region String
/// <summary>
/// 添加单个
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expireMinutes">过期时间,单位:分钟</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool StringSet(CacheFolderEnum folder, string key, string value, int expireMinutes = , int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, value, TimeSpan.FromMinutes(expireMinutes));
} /// <summary>
/// 批量添加
/// </summary>
/// <param name="folder">目录</param>
/// <param name="keysStr">键</param>
/// <param name="valuesStr">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool StringSet(CacheFolderEnum folder, string[] keysStr, string[] valuesStr, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var count = keysStr.Length;
var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
for (int i = ; i < count; i++)
{
keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(string.IsNullOrEmpty(fd) ? keysStr[i] : fd + ":" + keysStr[i], valuesStr[i]);
}
return Manager.GetDatabase(db).StringSet(keyValuePair);
} /// <summary>
/// 添加对象
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="obj">值</param>
/// <param name="expireMinutes">过期时间,单位:分钟</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool StringSet<T>(CacheFolderEnum folder, string key, T obj, int expireMinutes = , int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
string json = ConvertJson(obj);
return Manager.GetDatabase(db).StringSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, json, TimeSpan.FromMinutes(expireMinutes));
} /// <summary>
/// 获取单个
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static string StringGet(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
} /// <summary>
/// 获取多个
/// </summary>
/// <param name="folder">目录</param>
/// <param name="keys">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static RedisValue[] StringGet(CacheFolderEnum folder, List<string> keys, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).StringGet(ConvertRedisKeys(keys, fd));
} /// <summary>
/// 获取对象
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static T StringGet<T>(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
string value = Manager.GetDatabase(db).StringGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
return ConvertObj<T>(value);
}
#endregion #region List
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="start">索引开始</param>
/// <param name="stop">索引结束</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static List<T> ListRange<T>(CacheFolderEnum folder, string key, long start = , long stop = -, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var value = Manager.GetDatabase(db).ListRange(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
return ConvetList<T>(value);
} /// <summary>
/// 获取指定
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="index">索引</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static T ListGetByIndex<T>(CacheFolderEnum folder, string key, long index, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var value = Manager.GetDatabase(db).ListGetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index);
return ConvertObj<T>(value);
} /// <summary>
/// 替换指定
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="index">索引</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
public static void ListSetByIndex<T>(CacheFolderEnum folder, string key, long index, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
Manager.GetDatabase(db).ListSetByIndex(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, index, ConvertJson(value));
} /// <summary>
/// 删除指定
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="count">count > 0: Remove elements equal to value moving from head to tail.count 小于 0: Remove elements equal to value moving from tail to head.count = 0: Remove all elements equal to value.</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListRemove<T>(CacheFolderEnum folder, string key, T value, long count = , int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value), count);
} /// <summary>
/// 指定位置之后插入
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="pivot">位置</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListInsertAfter<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListInsertAfter(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
} /// <summary>
/// 指定位置之前插入
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="pivot">位置</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListInsertBefore<T>(CacheFolderEnum folder, string key, T pivot, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListInsertBefore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(pivot), ConvertJson(value));
} /// <summary>
/// 入栈(后插入的在List前面)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
public static long ListLeftPush<T>(CacheFolderEnum folder, string key, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
} /// <summary>
/// 批量入栈(后插入的在List前面)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="values">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListLeftPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
return Manager.GetDatabase(db).ListLeftPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
} /// <summary>
/// 出栈(删除最前面的一个元素并返回)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static T ListLeftPop<T>(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var value = Manager.GetDatabase(db).ListLeftPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
return ConvertObj<T>(value);
} /// <summary>
/// 入队(后插入的在List后面)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
public static long ListRightPush<T>(CacheFolderEnum folder, string key, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson(value));
} /// <summary>
/// 批量入队(后插入的在List后面)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="values">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListRightPush<T>(CacheFolderEnum folder, string key, List<T> values, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
return Manager.GetDatabase(db).ListRightPush(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
} /// <summary>
/// 出队(删除最后面的一个元素并返回)
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static T ListRightPop<T>(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var value = Manager.GetDatabase(db).ListRightPop(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
return ConvertObj<T>(value);
} /// <summary>
/// 获取个数
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long ListLength(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).ListLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
}
#endregion #region Hash
/// <summary>
/// 添加
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKey">元素的键</param>
/// <param name="t">实体</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool HashSet<T>(CacheFolderEnum folder, string key, string dataKey, T t, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).HashSet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey, ConvertJson(t));
} /// <summary>
/// 获取特定
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKey">元素的键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static T HashGet<T>(CacheFolderEnum folder, string key, string dataKey, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
string value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
return ConvertObj<T>(value);
} /// <summary>
/// 批量获取
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKeys">元素的键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static List<T> HashGet<T>(CacheFolderEnum folder, string key, RedisValue[] dataKeys, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var value = Manager.GetDatabase(db).HashGet(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys);
return ConvetList<T>(value);
} /// <summary>
/// 获取所有
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static HashEntry[] HashGetAll<T>(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).HashGetAll(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
} /// <summary>
/// 删除特定
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKey">元素的键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool HashDelete(CacheFolderEnum folder, string key, string dataKey, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
} /// <summary>
/// 批量删除
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKeys">元素的键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long HashDelete(CacheFolderEnum folder, string key, List<RedisValue> dataKeys, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).HashDelete(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKeys.ToArray());
} /// <summary>
/// 是否存在
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="dataKey">元素的键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool HashExists(CacheFolderEnum folder, string key, string dataKey, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).HashExists(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, dataKey);
}
#endregion #region Zset
/// <summary>
/// 添加
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="score">排序列</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool SortedSetAdd<T>(CacheFolderEnum folder, string key, T value, double score, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).SortedSetAdd(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value), score);
} /// <summary>
/// 获取
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="start">索引开始</param>
/// <param name="stop">索引结束</param>
/// <param name="order">排序方式</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static List<T> SortedSetRangeByRank<T>(CacheFolderEnum folder, string key, long start = , long stop = -, Order order = Order.Ascending, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var values = Manager.GetDatabase(db).SortedSetRangeByRank(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop, order);
return ConvetList<T>(values);
} /// <summary>
/// 删除
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static bool SortedSetRemove<T>(CacheFolderEnum folder, string key, T value, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, ConvertJson<T>(value));
} /// <summary>
/// 批量删除(根据对象)
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="values">对象</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long SortedSetRemove<T>(CacheFolderEnum folder, string key, List<T> values, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
var redisValues = values.Select(m => (RedisValue)ConvertJson(m)).ToArray();
return Manager.GetDatabase(db).SortedSetRemove(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, redisValues);
} /// <summary>
/// 批量删除(根据score删除)
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="start">开始</param>
/// <param name="stop">结束</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long SortedSetRemoveRangeByScore(CacheFolderEnum folder, string key, int start, int stop, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).SortedSetRemoveRangeByScore(string.IsNullOrEmpty(fd) ? key : fd + ":" + key, start, stop);
} /// <summary>
/// 获取总数
/// </summary>
/// <param name="folder">目录</param>
/// <param name="key">键</param>
/// <param name="db">库,默认读取配置文件</param>
/// <returns></returns>
public static long SortedSetLength(CacheFolderEnum folder, string key, int db = -)
{
db = GetOperationDB(db);
string fd = GetDescription(folder);
return Manager.GetDatabase(db).SortedSetLength(string.IsNullOrEmpty(fd) ? key : fd + ":" + key);
}
#endregion }
}
String
RedisHelper.StringSet(CacheFolderEnum.Folder1, "string", "hello world", 60);
效果:
List
RedisHelper.ListRightPush(CacheFolderEnum.Folder2, "list", student1);
RedisHelper.ListRightPush(CacheFolderEnum.Folder2, "list", student2);
RedisHelper.ListRightPush(CacheFolderEnum.Folder2, "list", student3);
效果:
Hash
RedisHelper.HashSet<Student>(CacheFolderEnum.Folder3, "hash", "h1", student1);
RedisHelper.HashSet<Student>(CacheFolderEnum.Folder3, "hash", "h2", student2);
效果:
SortedSet
RedisHelper.SortedSetAdd<Student>(CacheFolderEnum.Folder3, "zsort", student1, 111);
RedisHelper.SortedSetAdd<Student>(CacheFolderEnum.Folder3, "zsort", student2, 99);
RedisHelper.SortedSetAdd<Student>(CacheFolderEnum.Folder3, "zsort", student3, 100);
效果:(有序集合。不管插入顺序,始终按照score排好顺序)
注:
1.把要操作的目录写到枚举里,方便程序统一调用,不容易出错。如果多个目录嵌套就在Description上写[Description("一级目录:二级目录:三级目录")]
2.配置文件里的redis_store_db节点表示默认存在哪个库里,帮助类的每个方法也单独接收db参数,方便个性化存储到特定库
四、其他
源码:点击下载 环境是VS 2013,如果跑不起来自行把cs里的代码拷出来编译运行
爬虫可耻,本文原始链接:http://www.cnblogs.com/oppoic/p/6165581.html
参考资料:
怎样在Redis通过StackExchange.Redis 存储集合类型List
------------------------------------------补充:更新后的RedisHelper用法有点变化--------------------------------------------
private void button3_Click(object sender, EventArgs e)
{
RedisHelper.StringSet( "string", "hello world", 1);
RedisHelper.StringSet("string", "hello world1",1,RedisFolderEnum.Folder1,RedisDBEnum.Two); List<Student> list = new List<Student>();
list.Add(new Student() { NO = 001,Name = "001Name", Age=21});
list.Add(new Student() { NO = 002, Name = "002Name", Age = 21 });
list.Add(new Student() { NO = 003, Name = "003Name", Age = 21 }); RedisHelper.ListRightPush<Student>("student", list, RedisFolderEnum.Root, RedisDBEnum.Three); ///获取第2个
var stu=RedisHelper.ListGetByIndex<Student>("student", 1,RedisFolderEnum.Root,RedisDBEnum.Three); var ret=RedisHelper.HashSet<Student>("studentHash", stu.Name, stu, RedisFolderEnum.Root, RedisDBEnum.Four); stu = RedisHelper.ListGetByIndex<Student>("student", 2, RedisFolderEnum.Root, RedisDBEnum.Three);
var ret1 = RedisHelper.HashSet<Student>("studentHash", stu.Name, stu, RedisFolderEnum.Root, RedisDBEnum.Four);
var tmpS=RedisHelper.HashGet<Student>("studentHash", new StackExchange.Redis.RedisValue[] { "002Name","003Name" }, RedisFolderEnum.Root, RedisDBEnum.Four); var temList=RedisHelper.ListRange<Student>("student", 0, -1, RedisFolderEnum.Root, RedisDBEnum.Three);
}
}
public class Student
{
public int NO { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
From:http://www.cnblogs.com/xuejianxiyang/p/7084005.html
[转]Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager的更多相关文章
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager Redis缓存服务器是一款key/value数据库,读11 ...
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...
- C#版-Redis缓存服务器在Windows下的使用
Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...
- linux中的redis缓存服务器
Linux中的Redis缓存服务器 一.Redis基础部分: 1.redis介绍与安装比mysql快10倍以上 *****************redis适用场合**************** 1 ...
- windows下架设SVN服务器并设置开机启动
原文:windows下架设SVN服务器并设置开机启动 1.安装SVN服务器,到http://subversion.apache.org/packages.html上下载windows版的SVN,并安装 ...
- 如何在Windows下安装Tomcat服务器
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选服务器.在Windows下安装 ...
- windows 下搭建git服务器,及问题处理。
最近要做一个源码管理服务器,权衡了一下还是git最适合,搭建服务器前看了网上一些windows下搭建git服务器的帖子,感觉还比较简单,没有太多需要配置的地方,于是开始动手. 我选择的是 gitfor ...
- windows下搭建nginx-rtmp服务器
windows下搭建nginx-rtmp服务器 windows下搭建nginx-rtmp服务器 准备工作 安装MinGW 安装Mercurial 安装strawberryperl 安装nasm 下载n ...
- 在windows下远程访问linux服务器
在网络性能.安全性.可管理性上,Linux有着其他系统无法比拟的强大优势,而服务器对这些方面要求特别高,因此Linux常常被用来做服务器使用.而当我们需要维护linux服务器的时候,就需要远程访问li ...
随机推荐
- Hacking Lambda Expressions in Java
Hacking Lambda Expressions in Javahttps://dzone.com/articles/hacking-lambda-expressions-in-java At t ...
- linux中ls命令使用选项
ls:英文全名:List即列表的意思 -a 列出目录下的所有文件,包括以 . 开头的隐含文件.-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出.-c 输出文件的 i 节 ...
- 串口通信n
1.USART_Init(参数1,参数2) 串口配置步骤 1.串口时钟使能,GPIO使能 2.串口复位 3.端口模式设置GPIO_Init() 4.串口参数初始化USART_Init() 5,使能串口 ...
- HTML学习-02
1.本次学习主要是css和页面的跳转 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- 文献综述十四:基于Oracle11g的超市进销存管理系统设计与实现
一.基本信息 标题:基于Oracle11g的超市进销存管理系统设计与实现 时间:2016 出版源:技术创新 文件分类:对数据库的研究 二.研究背景 为超市设计开发的超市管理系统,采用的是 VC+ Or ...
- 03-oracle数值函数
--round(数值,想要保留的位数如1.2.3),作用:保留小数取值时四舍五入select round(678.656) 不保留小数,round(678.456,1) 保留一位小数,round(67 ...
- jQuery 学习笔记(TryjQuery)
第一集.页面加载完成后执行 js 代码: $(document).ready(function(){ $("h1").text("Where to?"); }) ...
- 【转】selenium自动化测试用例需要关注的几点(一)
自动化测试设计简介 注:参看文章地址 我们在本章提供的信息,对自动化测试领域的新人和经验丰富的老手都是有用的.本篇中描述最常见的自动化测试类型, 还描述了可以增强您的自动化测试套件可维护性和扩展性的 ...
- ubuntu下安装Sublime Text2
1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用,如linjiqin@ubuntu:~$ sudo ...
- haproxy 503 service unavailable no server is avaible to handle this request
原因: haproxy 对server检测时是对默认页面进行检测,如果没有默认错误,则直接报503错误,不会转发到相应的server上去. 解决方法: 添加 option httpchk 检测页面位置 ...