发现博客园中好多大牛在介绍自己的开源项目是很少用到缓存,比如Memcached、Redis、mongodb等,今天得空抽时间把Redis缓存研究了一下,写下来总结一下,跟大家一起分享 一下。由于小弟水平有限又是第一次接触Redis,有些的不对的地方欢迎指出纠正。
1、 下载安装Redis
下载地址:https://github.com/MSOpenTech/Redis。
2、 安装Redis
在下载的文件中找到bin并打开。
- redis-server.exe:服务程序(目前我们只用到这一个)
- redis-check-dump.exe:本地数据库检查
- redis-check-aof.exe:更新日志检查
- redis-benchmark.exe:性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询 (类似于 Apache 的ab 工具).
Redis有很多配置参数,这里就不介绍了,有兴趣的朋友可以到网上查查。
双击redis-server.exe或者在命令窗口中输入D:\redis-2.6\redis-server.exe即可启动Redis服务。
注意端口号:Port:6379 在我们的程序中会用到该端口号。
3、 数据读、写、删测试,并实现list存数。
首先引用下面四个DLL文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using ServiceStack.Redis;
using ServiceStack.Text;
using ServiceStack.Redis.Generic; namespace RedisStudy
{
public class RedisHelper:IDisposable
{
public RedisClient Redis = new RedisClient("127.0.0.1", 6379);
//缓存池
PooledRedisClientManager prcm = new PooledRedisClientManager(); //默认缓存过期时间单位秒
public int secondsTimeOut = 30 * 60; /// <summary>
/// 缓冲池
/// </summary>
/// <param name="readWriteHosts"></param>
/// <param name="readOnlyHosts"></param>
/// <returns></returns>
public static PooledRedisClientManager CreateManager(
string[] readWriteHosts, string[] readOnlyHosts)
{
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
new RedisClientManagerConfig
{
MaxWritePoolSize = readWriteHosts.Length * 5,
MaxReadPoolSize = readOnlyHosts.Length * 5,
AutoStart = true,
});// { RedisClientFactory = (IRedisClientFactory)RedisCacheClientFactory.Instance.CreateRedisClient("127.0.0.1", 6379) };
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="OpenPooledRedis">是否开启缓冲池</param>
public RedisHelper(bool OpenPooledRedis=false)
{ if (OpenPooledRedis)
{
prcm = CreateManager(new string[] { "127.0.0.1:6379" }, new string[] { "127.0.0.1:6379" });
Redis = prcm.GetClient() as RedisClient;
}
} #region Key/Value存储
/// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">缓存建</param>
/// <param name="t">缓存值</param>
/// <param name="timeout">过期时间,单位秒,-1:不过期,0:默认过期时间</param>
/// <returns></returns>
public bool Set<T>(string key, T t, int timeout = 0)
{
if (timeout >= 0)
{
if (timeout > 0)
{
secondsTimeOut = timeout;
}
Redis.Expire(key, secondsTimeOut);
} return Redis.Add<T>(key, t);
}
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
return Redis.Get<T>(key);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(string key)
{
return Redis.Remove(key);
} public bool Add<T>(string key, T t, int timeout)
{
if (timeout >= 0)
{
if (timeout > 0)
{
secondsTimeOut = timeout;
}
Redis.Expire(key, secondsTimeOut);
}
return Redis.Add<T>(key, t);
}
#endregion #region 链表操作
/// <summary>
/// 根据IEnumerable数据添加链表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listId"></param>
/// <param name="values"></param>
/// <param name="timeout"></param>
public void AddList<T>(string listId, IEnumerable<T> values, int timeout = 0)
{
Redis.Expire(listId,60);
IRedisTypedClient<T> iredisClient = Redis.As<T>();
if (timeout >= 0)
{
if (timeout > 0)
{
secondsTimeOut = timeout;
}
Redis.Expire(listId, secondsTimeOut);
}
var redisList = iredisClient.Lists[listId];
redisList.AddRange(values);
iredisClient.Save();
}
/// <summary>
/// 添加单个实体到链表中
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listId"></param>
/// <param name="Item"></param>
/// <param name="timeout"></param>
public void AddEntityToList<T>(string listId, T Item, int timeout = 0)
{
IRedisTypedClient<T> iredisClient = Redis.As<T>();
if (timeout >= 0)
{
if (timeout > 0)
{
secondsTimeOut = timeout;
}
Redis.Expire(listId, secondsTimeOut);
}
var redisList = iredisClient.Lists[listId];
redisList.Add(Item);
iredisClient.Save();
}
/// <summary>
/// 获取链表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listId"></param>
/// <returns></returns>
public IEnumerable<T> GetList<T>(string listId)
{
IRedisTypedClient<T> iredisClient = Redis.As<T>();
return iredisClient.Lists[listId];
}
/// <summary>
/// 在链表中删除单个实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listId"></param>
/// <param name="t"></param>
public void RemoveEntityFromList<T>(string listId, T t)
{
IRedisTypedClient<T> iredisClient = Redis.As<T>();
var redisList = iredisClient.Lists[listId];
redisList.RemoveValue(t);
iredisClient.Save();
}
/// <summary>
/// 根据lambada表达式删除符合条件的实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listId"></param>
/// <param name="func"></param>
public void RemoveEntityFromList<T>(string listId, Func<T,bool> func)
{
using (IRedisTypedClient<T> iredisClient = Redis.As<T>())
{
var redisList = iredisClient.Lists[listId];
T value = redisList.Where(func).FirstOrDefault();
redisList.RemoveValue(value);
iredisClient.Save();
}
}
#endregion
//释放资源
public void Dispose()
{
if (Redis != null)
{
Redis.Dispose();
Redis = null;
}
GC.Collect(); }
}
}
4、 测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading; namespace RedisStudy
{
public class User
{
public string Name { get; set; }
public int Id { get; set; }
} class Program
{
static RedisHelper redisHelper = new RedisHelper(true); static void Main(string[] args)
{
User u = new User() { Name = "eric1", Id = 1 };
redisHelper.Set<User>("user", u);//设置缓存
u=redisHelper.Get<User>("user");
Console.WriteLine(u.Name); List<User> list = new List<User>()
{
new User(){Name="eric1",Id=1},
new User(){Name="eric2",Id=2}
}; redisHelper.Remove("list1");//删除缓存
redisHelper.AddList<User>("list1", list);//添加缓存链表
redisHelper.AddEntityToList<User>("list1", new User() { Name = "eric3",Id=3 });
list = redisHelper.GetList<User>("list1").ToList();
Console.WriteLine(list.Count); redisHelper.RemoveEntityFromList<User>("list1", list[0]);
redisHelper.RemoveEntityFromList<User>("list1", it => it.Id == 2);
list = redisHelper.GetList<User>("list1").ToList();
Console.WriteLine(list.Count); redisHelper.Dispose();
Console.ReadLine(); } }
}
整篇文章写的比较仓促,很多基本点都没有说明,有不懂的请百度一下。
如果你有好的学习NoSQl的资料欢迎交流。
Redis其他操作,点击查看
点击下载源码
http://www.cnblogs.com/lc-chenlong/p/3221003.html