using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Memcached.ClientLibrary;
namespace DSWebMobileService
{
public class CacheData
{
///
/// 缓存是否存在
///
///
///
///
private static bool IsCache(MemcachedClient pMC, string pKey)
{
if (pMC.KeyExists(pKey))
{
return true;
}
else
{
return false;
}
}
///
/// 覆盖缓存
///
///
///
///
/*public static bool StoreCache(string pKey, object pObject)
{
MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = true;
bool _result = false;
if (IsCache(mc, pKey))
{
if (mc.Get(pKey) == null)
{
mc.Set(pKey, pObject, DateTime.Now.AddSeconds(20));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
}
else
{
mc.Replace(pKey, pObject, DateTime.Now.AddSeconds(20));//缓存存在,强行覆盖 DateTime.Now.AddMinutes(10)
}
_result = true;
}
else
{
mc.Add(pKey, pObject, DateTime.Now.AddSeconds(20));//第一次加载缓存 DateTime.Now.AddMinutes(10)
_result = true;
}
return _result;
}*/
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
{
//DataTable dttemp = new DataTable();
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, "'aaa':'a','bbb':'b'", 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 ;
//int iii = dtResult.Rows.Count;
return _result;
}
///
/// 清除缓存
///
///
///
public static bool RemoveCache(string pKey)
{
MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = true;
return mc.Delete(pKey);
}
}
}