|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Caching.Memory;
-
- namespace TelpoPush.Fence.Worker.Service.Cache
- {
- internal class MemoryCacheUtil
- {
- static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
-
-
-
-
-
- public static void Set(string key, object value)
- {
- if (key != null)
- {
- cache.Set(key, value);
- }
- }
-
-
-
-
-
-
- public static void Set(string key, object value, int expires)
- {
- if (key != null)
- {
- cache.Set(key, value, new MemoryCacheEntryOptions()
-
- .SetSlidingExpiration(TimeSpan.FromSeconds(expires)));
- }
- }
-
-
-
-
-
-
- public static object Get(string key)
- {
- object val = null;
- if (key != null && cache.TryGetValue(key, out val))
- {
-
- return val;
- }
- else
- {
- return default(object);
- }
- }
-
-
-
-
-
-
-
- public static T Get<T>(string key)
- {
- object obj = Get(key);
- return obj == null ? default(T) : (T)obj;
- }
-
-
-
-
-
-
- public static void Remove(string key)
- {
- cache.Remove(key);
- }
- }
- }
|