コード例 #1
0
        /// <summary>
        ///     向列表尾添加元素,若 key 对应无列表缓存,则自动创建并添加一个元素。
        /// </summary>
        /// <param name="key">主键。</param>
        /// <param name="value">要缓存的值。</param>
        /// <param name="options">缓存时间设置,为 NULL 表示永久。</param>
        public async Task RightPushListCacheAsync(string key, string value, KolibreCacheOptions options)
        {
            ConditionalValue <CacheWrapper> state = await StateManager.TryGetStateAsync <CacheWrapper>(key);

            CacheWrapper cacheWrapper = new CacheWrapper(CacheType.List);

            if (state.HasValue)
            {
                if (cacheWrapper.CacheType != CacheType.List)
                {
                    throw new InvalidOperationException($"Operation against a key holding the wrong type '{cacheWrapper.CacheType}'.");
                }

                if (!state.Value.IsExpired())
                {
                    cacheWrapper = state.Value;
                }
            }

            List <string> caches;

            try
            {
                caches = cacheWrapper.Cache.FromJson <List <string> >() ?? new List <string>();
            }
            catch
            {
                throw new InvalidOperationException($"Operation against a key holding the wrong type '{cacheWrapper.CacheType}'.");
            }

            caches.Insert(caches.Count, value);
            CacheWrapper result = cacheWrapper.SetCache(caches.ToJson(), CacheType.List, options);

            await StateManager.SetStateAsync(key, result);
        }
コード例 #2
0
        /// <summary>
        ///     设置单个实例缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="value">缓存值。</param>
        /// <param name="options">缓存有效期配置</param>
        public async Task SetTeacherSmartCacheAsync(string key, Teacher value, KolibreCacheOptions options)
        {
            CacheWrapper <Teacher> cacheWrapper = CacheWrapper <Teacher> .BuildCacheWrapper(new List <Teacher> {
                value
            }, CacheType.Object, options);

            await StateManager.SetStateAsync(key, cacheWrapper);
        }
コード例 #3
0
        /// <summary>
        ///     刷新缓存有效期。
        /// </summary>
        /// <param name="partitionId">缓存分区Id,不同Id对应不同区。</param>
        /// <param name="key">缓存键。</param>
        /// <param name="options">缓存有效期配置</param>
        public async Task RefreshAsync(string partitionId, string key, KolibreCacheOptions options)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (partitionId.IsNullOrEmpty())
            {
                partitionId = s_defaultPartitionId;
            }

            ISmartCacheActor actor = ActorProxy.Create <ISmartCacheActor>(new ActorId(partitionId), s_serviceUri);
            await actor.RefreshAsync(key, options);
        }
コード例 #4
0
        /// <summary>
        ///     刷新列表缓存。
        /// </summary>
        /// <param name="key">主键。</param>
        /// <param name="options">The options.</param>
        public async Task RefreshListCacheAsync(string key, KolibreCacheOptions options)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ICacheActor cacheActor = ActorProxy.Create <ICacheActor>(s_actorId, s_serviceUri);
            await cacheActor.RefreshListCacheAsync(key, options);
        }
コード例 #5
0
        /// <summary>
        ///     向列表尾添加元素,若 key 对应无列表缓存,则自动创建并添加一个元素。
        /// </summary>
        /// <param name="key">主键。</param>
        /// <param name="value">要缓存的值。</param>
        /// <param name="options">缓存时间设置,为 NULL 表示永久。</param>
        public async Task RightPushListCacheAsync <T>(string key, T value, KolibreCacheOptions options)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            ICacheActor cacheActor = ActorProxy.Create <ICacheActor>(s_actorId, s_serviceUri);
            await cacheActor.RightPushListCacheAsync(key, value.ToJson(), options);
        }
コード例 #6
0
        /// <summary>
        ///     刷新缓存有效期。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="options">缓存有效期配置</param>
        public async Task RefreshAsync(string key, KolibreCacheOptions options)
        {
            ConditionalValue <CacheWrapper <Student> > result = await StateManager.TryGetStateAsync <CacheWrapper <Student> >(key);

            if (!result.HasValue)
            {
                return;
            }

            CacheWrapper <Student> cacheWrapper = result.Value;

            if (cacheWrapper.IsExpired())
            {
                await StateManager.RemoveStateAsync(key);

                return;
            }

            await StateManager.SetStateAsync(key, cacheWrapper.Refresh(options));
        }
コード例 #7
0
        /// <summary>
        ///     刷新列表缓存。
        /// </summary>
        /// <param name="key">主键。</param>
        /// <param name="options">The options.</param>
        public async Task RefreshListCacheAsync(string key, KolibreCacheOptions options)
        {
            ConditionalValue <CacheWrapper> state = await StateManager.TryGetStateAsync <CacheWrapper>(key);

            if (!state.HasValue)
            {
                return;
            }

            CacheWrapper cacheWrapper = state.Value;

            if (cacheWrapper.IsExpired())
            {
                await StateManager.RemoveStateAsync(key);

                return;
            }

            await StateManager.SetStateAsync(key, cacheWrapper.Refresh(options));
        }
コード例 #8
0
        /// <summary>
        ///     设置列表实例缓存。
        /// </summary>
        /// <param name="partitionId">缓存分区Id,不同Id对应不同区。</param>
        /// <param name="key">缓存键。</param>
        /// <param name="values">缓存值。</param>
        /// <param name="options">缓存有效期配置</param>
        public async Task SetSmartCacheAsync(string partitionId, string key, List <Student> values, KolibreCacheOptions options)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (values == null || values.Count == 0)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (partitionId.IsNullOrEmpty())
            {
                partitionId = s_defaultPartitionId;
            }

            ISmartCacheActor actor = ActorProxy.Create <ISmartCacheActor>(new ActorId(partitionId), s_serviceUri);
            await actor.SetSmartCachesAsync(key, values, options);
        }
コード例 #9
0
        /// <summary>
        ///     设置列表实例缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="values">缓存值。</param>
        /// <param name="options">缓存有效期配置</param>
        public async Task SetSmartCachesAsync(string key, List <Student> values, KolibreCacheOptions options)
        {
            CacheWrapper <Student> cacheWrapper = CacheWrapper <Student> .BuildCacheWrapper(values, CacheType.Object, options);

            await StateManager.SetStateAsync(key, cacheWrapper);
        }
コード例 #10
0
 /// <summary>
 ///     设置缓存。
 /// </summary>
 /// <param name="key">缓存的主键。</param>
 /// <param name="value">要缓存的值。</param>
 /// <param name="options">缓存时间设置,为 NULL 表示永久。</param>
 public async Task SetCacheAsync(string key, string value, KolibreCacheOptions options)
 {
     CacheWrapper cacheWrapper = CacheWrapper.BuildCacheWrapper(value, CacheType.Normal, options);
     await StateManager.SetStateAsync(key, cacheWrapper);
 }
コード例 #11
0
        /// <summary>
        ///     设置 Hash 缓存。
        /// </summary>
        /// <param name="firstKey">主键。</param>
        /// <param name="secondKey">次键。</param>
        /// <param name="value">要缓存的值。</param>
        /// <param name="options">缓存时间设置,为 NULL 表示永久。</param>
        public async Task SetHashCacheAsync(string firstKey, string secondKey, string value, KolibreCacheOptions options)
        {
            ConditionalValue <CacheWrapper> state = await StateManager.TryGetStateAsync <CacheWrapper>(firstKey);

            CacheWrapper cacheWrapper = state.HasValue ? state.Value : new CacheWrapper(CacheType.Hash);

            if (cacheWrapper.CacheType != CacheType.Hash)
            {
                throw new InvalidOperationException($"Operation against a key holding the wrong type '{cacheWrapper.CacheType}'.");
            }

            // 若设置 Hash Cache 时,值不是 Dictionary<string,string> 类型,抛出异常
            Dictionary <string, string> hashCacheWrappers;

            try
            {
                hashCacheWrappers = cacheWrapper.Cache.FromJson <Dictionary <string, string> >() ?? new Dictionary <string, string>();
            }
            catch
            {
                throw new InvalidOperationException($"Operation against a key holding the wrong type '{cacheWrapper.CacheType}'.");
            }

            hashCacheWrappers[secondKey] = value;
            CacheWrapper result = cacheWrapper.SetCache(hashCacheWrappers.ToJson(), CacheType.Hash, options);

            await StateManager.SetStateAsync(firstKey, result);
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: Damon227/SFActorCache
        private static void Main(string[] args)
        {
            ICacheService             cacheService             = new CacheService();
            IStudentSmartCacheService studentSmartCacheService = new StudentSmartCacheService();
            ITeacherSmartCacheService teacherSmartCacheService = new TeacherSmartCacheService();

            Student student = new Student
            {
                Name       = "yuanchengman",
                Age        = 24,
                CreateTime = DateTimeOffset.Now
            };

            Student student1 = new Student
            {
                Name       = "yuanchengman1",
                Age        = 241,
                CreateTime = DateTimeOffset.Now
            };


            KolibreCacheOptions options = new KolibreCacheOptions
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(1000),
                SlidingExpiration  = TimeSpan.FromSeconds(3)
            };

            studentSmartCacheService.SetSmartCacheAsync(null, "key1", student, options).Wait();
            Student s1 = studentSmartCacheService.GetSmartCacheAsync(null, "key1").Result;

            Console.WriteLine(s1.ToJson());

            studentSmartCacheService.SetSmartCacheAsync(null, "key2", new List <Student> {
                student, student1
            }, options).Wait();
            List <Student> s2 = studentSmartCacheService.GetSmartCachesAsync(null, "key2").Result;

            Console.WriteLine(s2.ToJson());

            Teacher teacher = new Teacher
            {
                Name   = "957",
                IsGood = true
            };

            teacherSmartCacheService.SetSmartCacheAsync(null, "t1", teacher, options).Wait();
            List <Teacher> t1 = teacherSmartCacheService.GetSmartCachesAsync(null, "t1").Result;
            List <Student> t2 = studentSmartCacheService.GetSmartCachesAsync(null, "t1").Result;

            // 测试普通结构缓存

            //string key1 = "normal:yuanchengman";
            //cacheService.SetCacheAsync(key1, student, options).Wait();
            //for (int i = 0; i < 1000; i++)
            //{
            //    Student result1 = cacheService.GetCacheAsync<Student>(key1).Result;
            //    Console.WriteLine(i + ":" + result1.ToJson());
            //}

            Console.ReadLine();
        }
コード例 #13
0
        /// <summary>
        ///     设置 Hash 缓存。
        /// </summary>
        /// <param name="firstKey">主键。</param>
        /// <param name="secondKey">次键。</param>
        /// <param name="value">要缓存的值。</param>
        /// <param name="options">缓存时间设置,为 NULL 表示永久。</param>
        public async Task SetHashCacheAsync <T>(string firstKey, string secondKey, T value, KolibreCacheOptions options)
        {
            if (firstKey.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(firstKey));
            }

            if (secondKey.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(secondKey));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            ICacheActor cacheActor = ActorProxy.Create <ICacheActor>(s_actorId, s_serviceUri);
            await cacheActor.SetHashCacheAsync(firstKey, secondKey, value.ToJson(), options);
        }