zs
2024-11-20 5daec3779e682e1612f15d0f8cd67dd89909b5e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DataCapture_MA.Util
{
    /// <summary>
    /// 缓存相关的操作类
    /// Copyright (C) TBEA.WMS
    /// </summary>
    public class DataCache
    {
        private static IMemoryCache _cache;
 
        public DataCache(IMemoryCache cache)
        {
            _cache = cache;
        }
 
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            //System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            //return objCache[CacheKey];
            object value = null;
            _cache?.TryGetValue(CacheKey, out value);
            return value;
        }
 
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            //System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            //objCache.Insert(CacheKey, objObject);
            _cache?.Set(CacheKey, objObject, DateTimeOffset.Now.AddYears(10));
        }
 
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            //System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            //objCache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration);
            _cache?.Set(CacheKey, objObject, slidingExpiration);
        }
    }
}