# Caching **Repository Path**: deng.zz/DCommon.Caching ## Basic Information - **Project Name**: Caching - **Description**: 基于Microsoft.Extensions.Caching组件开发的缓存服务组件。 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 8 - **Forks**: 2 - **Created**: 2016-11-16 - **Last Updated**: 2024-12-04 ## Categories & Tags **Categories**: cache-modules **Tags**: None ## README #### redis缓存组件,特性如下: 1. 统一缓存调用入口:ICache,支持内存缓存(开发模式)和分布式缓存(生成模式)自由切换; 2. 兼容codis; 3. 使用分布式缓存时支持二级缓存(闪存)。 #### 调用如下: ##### 初始化: var services = new ServiceCollection(); services.AddMemoryCache(option => { }); services.AddSingleton(sp => new RedisService("localhost:32768")); services.AddDistributedRedisCache(option => { option.InstanceName = "local"; }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); ##### 调用代码: class PostManager { private readonly ICache m_Cache; public PostManager(CacheFactory cacheFactory) { m_Cache = cacheFactory.GetCache("Post"); } public Post Get(int id) { var key = CacheKey.NewCacheKeyString(id);//最终格式:local:Post:{id} var data = m_Cache.Get(key); if(data == null){ //TODO: get from database //m_Cache.Set(key, data); } return data; } public void Delete(int id) { var key = CacheKey.NewCacheKeyString(id); //TODO: delete from database m_Cache.Remove(key); } }