CommonUtils/CacheUtil.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Text;

namespace CommonUtils
{
    /// <summary>
    /// 缓存工具
    /// </summary>
    public static class CacheUtil
    {
        private static List<string> _accessedKeys = new List<string>();

        public static bool IsFirstAccess(string key)
        {
            if (_accessedKeys.Contains(key))
                return false;
            _accessedKeys.Add(key);
            return true;
        }

        #region 短存储

        /*
         * var cache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
         * cache.Set("hello", StringIdentifiers.ChinaInnovation);
         * cache.Get("hello").Print();
         * 框架自带方法,这边暂不使用
         * 无法转为长存储 设置过期时间ExpirationScanFrequency无效 程序结束时释放
         */

        private static StringObjectMap _cache { get; } = new StringObjectMap();

        public static void Set(string key, object value)
        => _cache.Set(key, value);

        public static void Save(string key, object value)
        => Set(key, value);

        public static TValue Get<TValue>(string key, TValue defaultValue = default)
        => (TValue)_cache.Get(key, defaultValue, false);

        public static string GetString(string key)
        => Get<string>(key);

        public static string Get(string key)
        => GetString(key);

        public static bool Contains(string key)
        => _cache.ContainsKey(key);

        public static bool Contains(string key, out string value)
        {
            value = Get(key);
            return Contains(key);
        }
        #endregion

        #region 长存储
        private static StringObjectMap _longCache { get; } = new StringObjectMap();
后面还有 44 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。