一、安装Redis
Redis官网下载地址:Redis,下载相应版本的Redis,在运行中输入cmd,然后把目录指向解压的Redis目录。
我是Windows 10 + C# 开发,所以找一个 Windows 版本就好
我们去找一个 Windows 的版本
Releases · microsoftarchive/redis · GitHub
Releases · tporadowski/redis · GitHubre
下载解压,然后启动
D:\Redis>redis-server.exe redis.windows.conf
看到如下,就已经好了
2016/07/01 09:17 14,265 Windows Service Documentation.docx
14 个文件 22,470,282 字节
2 个目录 40,509,333,504 可用字节
D:\Redis>redis-server.exe redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.100 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 22860
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[22860] 06 Jan 13:45:11.605 # Server started, Redis version 3.2.100
[22860] 06 Jan 13:45:11.609 * The server is now ready to accept connections on port 6379
[22860] 06 Jan 14:00:12.048 * 1 changes in 900 seconds. Saving...
[22860] 06 Jan 14:00:12.056 * Background saving started by pid 18424
[22860] 06 Jan 14:00:12.266 # fork operation complete
[22860] 06 Jan 14:00:12.267 * Background saving terminated with success
二、Redis 的 C# 应用
C# Helper 类
public class RedisHelper
{
private static RedisClient Redis = null;
static RedisHelper()
{
Redis = new RedisClient("127.0.0.1", 6379);
}
public static void SetMem<T>(T t, string key, int minute)
{
if (minute == 0)
{
Redis.Set<T>(key, t);
}
else
{
DateTime expiryTime = DateTime.Now.AddMinutes(minute);
Redis.Set<T>(key, t, expiryTime);
}
}
public static T GetMem<T>(string key)
{
return Redis.Get<T>(key);
}
/// <summary>
/// 插入DATASET缓存
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="item">缓存对象</param>
/// <param name="minute">过期时间(分钟)</param>
public static void SetMemByDataSet(string key, DataTable dt, int minute)
{
DateTime expiryTime = DateTime.Now.AddMinutes(minute);
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
System.IO.MemoryStream ms = new System.IO.MemoryStream();//创建内存流对象
formatter.Serialize(ms, dt);//把dt对象序列化到内存流
byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
ms.Close();//关闭内存流对象
ms.Dispose();//释放资源
if (minute == 0)
{
Redis.Set(key, buffer);
}
else
{
Redis.Set(key, buffer, expiryTime);
}
}
public static object Get(string key)
{
byte[] buffer = Redis.Get(key);
return GetObjFromBytes(buffer);
}
/// <summary>
/// 从二进制流得到对象(data专用,data数据要序列化为二进制才可保存
/// /// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
private static object GetObjFromBytes(byte[] buffer)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer))
{
stream.Position = 0;
System.Runtime.Serialization.IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Object reobj = bf.Deserialize(stream);
return reobj;
}
}
/// <summary>
/// 判断是否失效或没有
/// </summary>如果为false已失效
/// <param name="key"></param>
/// <returns></returns>
public static bool IsExpired(string key)
{
bool result = true;
byte[] buffer = Redis.Get(key);
if (buffer == null)
{
result = false;
}
return result;
}
public static DataTable GetMemByDataSet(string key)
{
var item = Get(key);
return (DataTable)item;
}
/// <summary>
/// 清空缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static bool Remove(string key)
{
return Redis.Remove(key);
}
/// <summary>
/// 清空所有缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static void FlushAll()
{
Redis.FlushAll();
}
}
三、查看数据
使用 redis-cli 就可以方便的查看数据
D:\Redis>redis-cli
127.0.0.1:6379> set test 1000
OK
127.0.0.1:6379> get test
"1000"