Redis String字符串
/// <summary>
/// 根据key获取T对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key) {
var valule = _db.StringGet(key);
if (valule.HasValue)
return JsonConvert.DeserializeObject<T>(valule.ToString());
return default(T);
}
/// <summary>
/// 根据key获取string对象
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string Get(string key) {
return _db.StringGet(key);
}
/// <summary>
/// 写人string
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public static bool Set(string key, object value, DateTime? expireTime = null) {
try {
var result = _db.StringSet(key, JsonConvert.SerializeObject(value));
_db.KeyExpire(key, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
return result;
}
catch (Exception ex) {
throw;
}
}
///递增
public static long Increment(string key, int amount)
{
try
{
return _db.StringIncrement(key, amount);
}
catch (Exception)
{
throw;
}
}