**
第一步:nuget包——ServiceStack.Redis
**
public RedisCache _iCache = new RedisCache();
public void test(){string tokenKey = "";Token authToken = _iCache.Get<Token>(tokenKey);if (authToken == null){authToken = new Token();authToken.SignToken = Guid.NewGuid().ToString().Replace("-", "");//设置tokenauthToken.ExpireTime = DateTime.Now.AddDays(1);//过期时间值_iCache.Insert(tokenKey, authToken, authToken.ExpireTime);}
}
/// <summary>
/// StackExchange Redis操作
/// </summary>
public class RedisCache
{int Default_Timeout = 600;//默认超时时间(单位秒)string address;JsonSerializerSettings jsonConfig = new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore };ConnectionMultiplexer connectionMultiplexer;IDatabase database;class CacheObject<T>{public int ExpireTime { get; set; }public bool ForceOutofDate { get; set; }public T Value { get; set; }}public RedisCache(){this.address = "==============连接地址===================";if (this.address == null || string.IsNullOrWhiteSpace(this.address.ToString()))throw new ApplicationException("配置文件中未找到RedisServer的有效配置");try{connectionMultiplexer = ConnectionMultiplexer.Connect(address);//RedisDBIdint dbID = 0;database = connectionMultiplexer.GetDatabase(dbID);}catch (Exception){throw new ApplicationException("连接异常");}}public RedisCache(string dbAddress, int dbID){this.address = dbAddress;if (this.address == null || string.IsNullOrWhiteSpace(this.address.ToString()))throw new ApplicationException("Redis地址异常");connectionMultiplexer = ConnectionMultiplexer.Connect(address);database = connectionMultiplexer.GetDatabase(dbID);}/// <summary>/// 连接超时设置/// </summary&