239 lines
6.2 KiB
C#
239 lines
6.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Web;
|
||
using System.Web.Caching;
|
||
using System.Collections;
|
||
|
||
|
||
namespace MyCode.Project.Infrastructure.Cache
|
||
{
|
||
public class SystemCache : IMyCodeCacheService
|
||
{
|
||
private static System.Web.Caching.Cache cache;
|
||
|
||
public static Dictionary<string, Queue> dicQueue;
|
||
|
||
public static Dictionary<string, long> dicIncr;
|
||
|
||
public SystemCache()
|
||
{
|
||
|
||
if (cache == null)
|
||
{
|
||
cache = HttpRuntime.Cache;
|
||
dicQueue = new Dictionary<string, Queue>();
|
||
dicIncr = new Dictionary<string, long>();
|
||
}
|
||
}
|
||
|
||
#region Set(设置绝对过期时间)
|
||
public void Set(string key, object cache_object, TimeSpan? cacheTime = null)
|
||
{
|
||
|
||
if (cacheTime == null || cacheTime.Value == TimeSpan.Zero) { cacheTime = new TimeSpan(0, 0, 30); }
|
||
|
||
//相对时间过期
|
||
//cache.Insert(key,cache_object,null, System.Web.Caching.Cache.NoAbsoluteExpiration,expiration,priority,null);
|
||
//绝对时间过期
|
||
//cache.Insert(key, cache_object, null,DateTime.Now.addti,expiration, System.Web.Caching.Cache.NoSlidingExpiration);
|
||
//var second = expiration.TotalSeconds;
|
||
if (cache_object == null) { return; }
|
||
|
||
cache.Insert(key, cache_object, null, DateTime.Now.AddSeconds(cacheTime.Value.TotalSeconds), System.Web.Caching.Cache.NoSlidingExpiration);
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region KeyExpire(设置缓存有效时长)(系统缓存无执行效果)
|
||
/// <summary>
|
||
/// 设置缓存有效时长(系统缓存无执行效果)
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="expiry"></param>
|
||
public void KeyExpire(string key, TimeSpan? expiry)
|
||
{
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
#region Get(根据key得到缓存值)
|
||
public object Get(string key)
|
||
{
|
||
return cache.Get(key);
|
||
}
|
||
#endregion
|
||
|
||
#region Get(获取一个具体的对象)
|
||
public T Get<T>(string key)
|
||
{
|
||
var obj = Get(key);
|
||
|
||
return (T)obj;
|
||
}
|
||
#endregion
|
||
|
||
#region Delete(按Key删除)
|
||
public void Delete(string key)
|
||
{
|
||
if (Exists(key))
|
||
{
|
||
cache.Remove(key);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Exists(判断缓存是否存在)
|
||
public bool Exists(string key)
|
||
{
|
||
if (cache[key] != null)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region GetCacheKeys(取得缓存所有Key)
|
||
public List<string> GetCacheKeys()
|
||
{
|
||
List<string> keys = new List<string>();
|
||
IDictionaryEnumerator ca = cache.GetEnumerator();
|
||
while (ca.MoveNext())
|
||
{
|
||
keys.Add(ca.Key.ToString());
|
||
}
|
||
return keys;
|
||
}
|
||
#endregion
|
||
|
||
#region Push(放入队列)
|
||
/// <summary>
|
||
/// 放入队列
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="data"></param>
|
||
public void Push(string key, object data)
|
||
{
|
||
if (!dicQueue.Any(p => p.Key == key))
|
||
{
|
||
dicQueue.Add(key, new Queue());
|
||
}
|
||
|
||
var queue = dicQueue[key];
|
||
|
||
queue.Enqueue(data);
|
||
}
|
||
#endregion
|
||
|
||
#region Push(放入队列)
|
||
/// <summary>
|
||
/// 放入队列
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="data"></param>
|
||
/// <param name="timeSpan">此参数无效,系统缓存没有指定过期时间,强制为永久</param>
|
||
public void Push(string key, object data, TimeSpan? timeSpan = null)
|
||
{
|
||
if (!dicQueue.Any(p => p.Key == key))
|
||
{
|
||
dicQueue.Add(key, new Queue());
|
||
|
||
}
|
||
|
||
var queue = dicQueue[key];
|
||
|
||
queue.Enqueue(data);
|
||
}
|
||
#endregion
|
||
|
||
#region Popup(获取第一条数据,并删除)
|
||
/// <summary>
|
||
/// Popup(获取第一条数据,并删除)
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public object Popup(string key)
|
||
{
|
||
if (!dicQueue.Any(p => p.Key == key))
|
||
{
|
||
dicQueue.Add(key, new Queue());
|
||
}
|
||
|
||
var queue = dicQueue[key];
|
||
|
||
return queue.Dequeue();
|
||
}
|
||
#endregion
|
||
|
||
#region Incr(自增1,返回自增后的值)
|
||
/// <summary>
|
||
/// 自增1,返回自增后的值
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public long Incr(string key)
|
||
{
|
||
if (!dicIncr.Any(p => p.Key == key))
|
||
{
|
||
dicIncr.Add(key, 1);
|
||
|
||
return 1;
|
||
}
|
||
|
||
dicIncr[key] = dicIncr[key] + 1;
|
||
|
||
return dicIncr[key];
|
||
}
|
||
#endregion
|
||
|
||
#region Decr(递减1,返回递减后的值)
|
||
/// <summary>
|
||
/// 递减1,返回递减后的值
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public long Decr(string key)
|
||
{
|
||
if (!dicIncr.Any(p => p.Key == key))
|
||
{
|
||
dicIncr.Add(key, 0);
|
||
|
||
return 0;
|
||
}
|
||
|
||
dicIncr[key] = dicIncr[key] - 1;
|
||
|
||
return dicIncr[key];
|
||
}
|
||
#endregion
|
||
|
||
#region SetInt(设置一个整型缓存)
|
||
/// <summary>
|
||
/// 设置一个整型缓存
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="value"></param>
|
||
public void SetIncr(string key, int value)
|
||
{
|
||
if (!dicIncr.Any(p => p.Key == key))
|
||
{
|
||
dicIncr.Add(key, value);
|
||
}
|
||
else
|
||
{
|
||
dicIncr[key] = value;
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|