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

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - 上海-天浩

软件工程的核心问题并非是编程 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