/// <summary> /// 从任务执行队列移除任务 /// </summary> /// <param name="pTask">执行任务</param> private static void RemoveToTaskQueue(CacheTask pTask) { if (String.IsNullOrWhiteSpace(pTask.Key)) { return; } string TaskIdentity = pTask.Key + pTask.TaskType.ToString(); if (m_TaskRecordQueue == null) { if (m_TaskRecordQueue == null) { m_TaskRecordQueue = new ConcurrentDictionary <string, CacheTask>(); } GC.SuppressFinalize(m_TaskRecordQueue); GC.KeepAlive(m_TaskRecordQueue); return; } // 及时键不存在也不会报错 m_TaskRecordQueue.TryRemove(TaskIdentity, out pTask); }
/// <summary> /// 根据传入的Url从任务执行队列移除任务 /// </summary> /// <param name="cacheUrl">分布缓存关键字</param> private static void RemoveToTaskQueueByCacheUrl(string cacheUrl) { CacheTask pTask = new CacheTask(); pTask.TaskType = CacheTaskType.AutoUpdateCache; pTask.Key = cacheUrl; RemoveToTaskQueue(pTask); pTask.TaskType = CacheTaskType.RemoveCache; RemoveToTaskQueue(pTask); }
/// <summary> /// 注册更新缓存的异步回调 /// </summary> /// <param name="cacheUrl">分布缓存关键字</param> /// <param name="updateCacheRelativeTimeSpan">自动更新缓存的时间戳</param> internal static void RegisterUpdateCacheObverse(string cacheUrl, TimeSpan updateCacheRelativeTimeSpan) { if (String.IsNullOrWhiteSpace(cacheUrl)) { return; } CacheTask pTask = new CacheTask(); pTask.ScheduledTime = DateTime.Now.Add(updateCacheRelativeTimeSpan); pTask.TaskType = CacheTaskType.AutoUpdateCache; pTask.Key = cacheUrl; AddToTaskQueue(pTask); }
/// <summary> /// 设置分布缓存内容 /// </summary> /// <param name="key">需要设置分布缓存关键字</param> /// <param name="cacheContent">分布缓存的页面内容</param> /// <param name="cacheRelativeExpiration">分布缓存过期时间戳</param> internal static bool SetCaches(string key, object cacheContent, TimeSpan cacheRelativeExpiration) { if (String.IsNullOrWhiteSpace(key) || cacheContent == null) { return(false); } if (m_CacheQueue == null) { m_CacheQueue = new ConcurrentDictionary <string, object>(); GC.SuppressFinalize(m_CacheQueue); GC.KeepAlive(m_CacheQueue); } int cacheUrlCount = (m_CacheQueue == null ? 0 : m_CacheVisitCount.Count); if (cacheUrlCount >= m_MaxCacheUrlCount) { ClearMemoryCaches(false); } object oldCacheContent = null; int cacheContentMemorySize = 0; if (m_CacheVisitCount == null) { m_CacheVisitCount = new ConcurrentDictionary <string, long>(); } GC.SuppressFinalize(m_CacheVisitCount); GC.KeepAlive(m_CacheVisitCount); m_CacheVisitCount.AddOrUpdate(key, 1, (updateUrl, visitCount) => { ++visitCount; return(visitCount); }); if (m_CacheQueue.TryGetValue(key, out oldCacheContent)) { if (oldCacheContent == cacheContent) { return(true); } if (m_CacheQueue.TryUpdate(key, cacheContent, oldCacheContent)) { cacheContentMemorySize = Marshal.SizeOf(cacheContent); Interlocked.Add(ref m_UsedMemorySize, cacheContentMemorySize); cacheContentMemorySize = Marshal.SizeOf(oldCacheContent); Interlocked.Add(ref m_UsedMemorySize, -1 * cacheContentMemorySize); } else { return(false); } } else { if (m_CacheQueue.TryAdd(key, cacheContent)) { cacheContentMemorySize = Marshal.SizeOf(cacheContent); Interlocked.Add(ref m_UsedMemorySize, cacheContentMemorySize); if (cacheRelativeExpiration != TimeSpan.Zero) { CacheTask pTask = new CacheTask(); pTask.ScheduledTime = DateTime.Now.Add(cacheRelativeExpiration); pTask.TaskType = CacheTaskType.RemoveCache; pTask.Key = key; AddToTaskQueue(pTask); } } else { return(false); } } if (m_MaxMemoryUsedSize <= m_UsedMemorySize) { ClearMemoryCaches(false); } return(true); }
/// <summary> /// 任务执行回调函数 /// </summary> /// <param name="args">未使用才参数, 值为null</param> internal static void RunTaskExecute(object args) { if (m_TaskExecuting) { return; } m_TaskExecuting = true; // 如果任务队列本身为空, 则不用再继续执行函数 if (m_TaskRecordQueue == null) { m_TaskRecordQueue = new ConcurrentDictionary <string, CacheTask>(); GC.SuppressFinalize(m_TaskRecordQueue); GC.KeepAlive(m_TaskRecordQueue); m_TaskExecuting = false; return; } if (m_TaskRecordQueue.Count == 0) { m_TaskExecuting = false; return; } List <KeyValuePair <string, long> > lstVisitRecord = new List <KeyValuePair <string, long> >(); foreach (KeyValuePair <string, CacheTask> taskRecord in m_TaskRecordQueue) { // 不能直接使用任务队列的Key, 任务队列的唯一标识是Key属性和TaskType的接口, 但添加到List<KeyValuePair<string, long>>中的必须是m_TaskRecordQueue的Key, 方便代码 if (m_TaskRecordQueue.TryGetValue(visitRecord.Key, out pTask)) if (m_CacheVisitCount.ContainsKey(taskRecord.Value.Key)) { lstVisitRecord.Add(new KeyValuePair <string, long>(taskRecord.Key, m_CacheVisitCount[taskRecord.Value.Key])); } else { lstVisitRecord.Add(new KeyValuePair <string, long>(taskRecord.Key, 0L)); } } // 默认排序顺序是升序排序, 此处为降序, 返回次数多的页面的任务先执行 lstVisitRecord.Sort(delegate(KeyValuePair <string, long> visitRecord1, KeyValuePair <string, long> visitRecord2) { return(-1 * visitRecord1.Value.CompareTo(visitRecord2.Value)); }); CacheTask pTask = null; foreach (KeyValuePair <string, long> visitRecord in lstVisitRecord) { if (m_TaskRecordQueue.TryGetValue(visitRecord.Key, out pTask)) { if (pTask.ScheduledTime.CompareTo(DateTime.Now) <= 0 && visitRecord.Value > 1L) { RemoveToTaskQueue(pTask); switch (pTask.TaskType) { case CacheTaskType.AutoUpdateCache: //if (!UpdateCache(pTask.Key)) //{ // pTask.ScheduledTime = DateTime.Now.AddSeconds(5.00); // AddToTaskQueue(pTask); //} //Thread.Sleep(m_AutoUpdateCacheSleepTime); break; case CacheTaskType.RemoveCache: RemoveCacheCallback(pTask.Key); break; default: break; } } } } lstVisitRecord.Clear(); lstVisitRecord = null; m_TaskExecuting = false; }