|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Memcached.ClientLibrary;
|
|
|
|
|
|
|
|
|
|
namespace HomeService.Utility
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class CacheDataUtil
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 缓存是否存在
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pMC"></param>
|
|
|
|
|
/// <param name="pKey"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static bool IsCache(MemcachedClient pMC, string pKey)
|
|
|
|
|
{
|
|
|
|
|
if (pMC.KeyExists(pKey))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 覆盖缓存
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pKey"></param>
|
|
|
|
|
/// <param name="pObject"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool StoreCache(string pKey, object pObject, int dateTimeType, int dateTimeValue) //dateTimeType: 0-seconds, 1-minutes, 2-hours
|
|
|
|
|
{
|
|
|
|
|
MemcachedClient mc = new MemcachedClient();
|
|
|
|
|
mc.EnableCompression = true;
|
|
|
|
|
bool _result = false;
|
|
|
|
|
if (IsCache(mc, pKey))
|
|
|
|
|
{
|
|
|
|
|
if (mc.Get(pKey) == null)
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_result = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_result = true;
|
|
|
|
|
}
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool DeleteCache(string pKey)
|
|
|
|
|
{
|
|
|
|
|
MemcachedClient mc = new MemcachedClient();
|
|
|
|
|
mc.EnableCompression = true;
|
|
|
|
|
bool _result = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (IsCache(mc, pKey))
|
|
|
|
|
{
|
|
|
|
|
mc.Delete(pKey);
|
|
|
|
|
_result = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_result = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_result = false;
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool StoreDataTableCache(string pKey, DataTable pObject, int dateTimeType, int dateTimeValue) //dateTimeType: 0-seconds, 1-minutes, 2-hours
|
|
|
|
|
{
|
|
|
|
|
MemcachedClient mc = new MemcachedClient();
|
|
|
|
|
mc.EnableCompression = true;
|
|
|
|
|
bool _result = false;
|
|
|
|
|
if (IsCache(mc, pKey))
|
|
|
|
|
{
|
|
|
|
|
if (mc.Get(pKey) == null)
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Set(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Replace(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_result = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (dateTimeType)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddSeconds(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddMinutes(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
mc.Add(pKey, pObject, DateTime.Now.AddHours(dateTimeValue));//第一次加载缓存 DateTime.Now.AddMinutes(10)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_result = true;
|
|
|
|
|
}
|
|
|
|
|
string dtResult = mc.Get(pKey) as string;
|
|
|
|
|
return _result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清除缓存
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pKey"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool RemoveCache(string pKey)
|
|
|
|
|
{
|
|
|
|
|
MemcachedClient mc = new MemcachedClient();
|
|
|
|
|
mc.EnableCompression = true;
|
|
|
|
|
return mc.Delete(pKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|