惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
量子位
Security Latest
Security Latest
P
Proofpoint News Feed
P
Privacy International News Feed
P
Palo Alto Networks Blog
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Vercel News
Vercel News
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
Kaspersky official blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
AWS News Blog
AWS News Blog
博客园 - Franky
爱范儿
爱范儿
T
Tor Project blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
小众软件
小众软件
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
W
WeLiveSecurity
SecWiki News
SecWiki News
L
LangChain Blog
I
InfoQ

博客园 - 上海-天浩

软件工程的核心问题并非是编程 C# 生产消费模式 C# ConcurrentQueue 处理多任务 c# 线程池多任务处理并返回值 最近忙的一个项目(B/S) 共享设备综合运营管理系统(自研) 自动充值机 面向对象的七种设计原则 SQL server 添加主外键约束 做过的项目 诗二首 诗三首 开源大全 C# PowerPoint操作的基本用法。 无锡质检局实验室管理系统 交互式电子白板 自己开发的 漂亮音乐播放器 钥匙齿形识别系统
ASP.NET WebApi(.Net Framework) 应用CacheManager
上海-天浩 · 2023-12-14 · via 博客园 - 上海-天浩

ASP.NET WebApi(.Net Framework) 应用CacheManager ,内存+Redis

1,WebApi 版本选.net4.6.2 以上版本
2,nuget包
Unity (4.0.0.1)
Unity.AspNet.WebApi (4.0.0.1)
CacheManager.Core(1.2)
CacheManager.Microsoft.Extensions.Caching.Memory
CacheManager.Microsoft.Extensions.Configuration
CacheManager.Serialization.Json
CacheManager.StackExchange.Redis
CacheManager.SystemRuntimeCaching

3,web.config 添加
<connectionStrings>
<add name="redisConnection" connectionString="ip:6379,password=****" />
</connectionStrings>

4, 启动位置,添加代码
protected void Application_Start()
{
var container = new UnityContainer();

AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

var cacheConfig = ConfigurationBuilder.BuildConfiguration(settings =>
{
settings.WithUpdateMode(CacheUpdateMode.Up)
.WithSystemRuntimeCacheHandle("inProcessCache")//内存缓存Handle
.WithExpiration(ExpirationMode.Sliding, TimeSpan.FromMinutes(60));
settings.WithJsonSerializer();
settings.WithRedisBackplane("redisConnection");
settings.WithRedisCacheHandle("redisConnection");

});

container.RegisterType(
typeof(ICacheManager<>),
new ContainerControlledLifetimeManager(),
new InjectionFactory(
(c, t, n) => CacheFactory.FromConfiguration(t.GetGenericArguments()[0], cacheConfig)));

}

5,Controller控制层
public class TestController : ApiController
{
[Dependency]
protected ICacheManager<Todo> TodoCache
{
get;
set;
}

6,测试代码
// GET api/Test/5
public string Get(int id)
{
Todo value1 = new Todo();
value1.Title = "test1-"+id.ToString();
TodoCache.Add("test1-" + id.ToString(), value1);


7,查看redis
推荐工具 Another Redis Desktop Manager