1.添加引用StackExchange.Redis.dll
2.Redis初始化放在ASP.NET应用程序文件Global.asax.cs的Application_Start中
3.使用举例
public List GetDepartments()
{
var user = this.GetUserInfo();
var key = “deptment:ALL”;
var cache = RedisHelper.Get(key);
if (cache != null)
{
return cache;
}
var depts = c6set.AsQueryable().Where(p => p.ZHR900104 != null).Select(p => new DepartmentPatialModel
{
Code = p.ZHR900104,
PCode = p.SZHR900104,
Name = p.STEXT
}).ToList();
RedisHelper.Set(key, depts, DateTime.Now.AddHours(2));
return depts;
}
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Casetek.DCS.Common
{
///
/// RedisHelper帮助类
///
public class RedisHelper
{
private static int initTimes = 3;//初始化次数
private static int timeOut = 60;//默认超时时间(单位秒)
private static string configUrl = null;
private static ConnectionMultiplexer connectionMultiplexer = null;
private static IServer server = null;
private static IDatabase database = null;
///
/// 初始化
///
public static void Init()
{
try
{
if (initTimes > 0)
{
--initTimes;
configUrl = CommonConstValue.RedisIPAndPassword;
if (!CommonConstValue.IsNormalDevelopmentEnvironment)
{
configUrl = "服务器IP,password=";
}
connectionMultiplexer = ConnectionMultiplexer.Connect(configUrl);
server = connectionMultiplexer.GetServer(configUrl.Split(',')[0] + ":6379");
database = connectionMultiplexer.GetDatabase();
}
}
catch
{
Init();
}
}
private static JsonSerializerSettings jsonConfig = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
};
public static object Get(string key)
{
return Get