1 Star 3 Fork 1

Bili执笔小白/CSharp_DistributedCache_Simple

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Program.cs 7.56 KB
一键复制 编辑 原始数据 按行查看 历史
Bili执笔小白 提交于 2024-01-12 17:24 +08:00 . 完成Memcached示例
#define MemCached // DMemoryCache, MemCached, Redis,NCache, SqlServer, MySql,PostgreSql
using Alachisoft.NCache.Caching.Distributed;
using Alachisoft.NCache.Common.Protobuf;
using Community.Microsoft.Extensions.Caching.PostgreSql;
using Enyim.Caching.Memcached;
using Microsoft.AspNetCore.Http.HttpResults;
using System;
using System.Collections.Generic;
using System.Configuration;
namespace MemoryCache_WebAPI
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMemoryCache(options => // 1、内存缓存(一般用于粘性会话或单机缓存;Microsoft.Extensions.Caching.Memory)
{
//options.SizeLimit = 100; // 缓存的最大大小为100份
options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); // 两秒钟查找一次过期项
options.CompactionPercentage = 0.2; // 缓存满了时候压缩20%的优先级较低的数据
options.TrackStatistics = false; // 设置是否跟踪内存缓存统计信息。 默认已禁用
options.TrackLinkedCacheEntries = false; // 设置是否跟踪链接条目。 默认已禁用
});
#region 分布式内存
#if DMemoryCache
builder.Services.AddDistributedMemoryCache(); // 2、分布式内存缓存(一般用于分布式会话或分布式内存存储;Microsoft.Extensions.Caching.Memory)
#elif MemCached
builder.Services.AddEnyimMemcached(options => // 2.1 分布式缓存-Memcached (EnyimMemcachedCore)
{
options.AddServer("127.0.0.1", 11211);
});
#elif Redis
builder.Services.AddStackExchangeRedisCache(options => // 2.2 分布式缓存-Redis (Microsoft.Extensions.Caching.StackExchangeRedis)
{
options.Configuration = "127.0.0.1:6379,password=Aa123456,ssl=False,abortConnect=False,defaultDatabase=1"; // 连接字符串;库db1
//options.InstanceName = "RedisInstance"; // 键值对组(不必须)
//options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions()
//{
// DefaultDatabase = 2,
// Password = "12345",
// ConnectTimeout = 5000, //设置建立连接到Redis服务器的超时时间为5000毫秒
// SyncTimeout = 5000, //设置对Redis服务器进行同步操作的超时时间为5000毫秒
// ResponseTimeout = 5000, //设置对Redis服务器进行操作的响应超时时间为5000毫秒
// Ssl = false, //设置启用SSL安全加密传输Redis数据
// //SslHost=""
// //SslProtocols = System.Security.Authentication.SslProtocols.Tls//还可以通过SslProtocols属性指定SSL具体用到的是什么协议,不过这个属性不是必须的
//};
//options.ConfigurationOptions.EndPoints.Add("192.168.1.105:6379");
});
#elif NCache
builder.Services.AddNCacheDistributedCache(options => // 2.3 分布式缓存-NCache (NCache.Microsoft.Extensions.Caching.OpenSource)
{
options.CacheName = "testcache"; // 缓存名
options.EnableLogs = true;
options.ExceptionsEnabled = true;
});
#elif SqlServer
builder.Services.AddDistributedSqlServerCache(options => // 2.4 分布式缓存-SqlServer (Microsoft.Extensions.Caching.SqlServer)
{
options.ConnectionString = "Data Source=(local);Initial Catalog=TestDB;User ID=sa;Password=Aa123456;Persist Security Info=True;Encrypt=True;TrustServerCertificate=True;"; // 连接字符串
options.SchemaName = "dbo"; // 数据库权限
options.TableName = "TestCache"; // 表名
options.DefaultSlidingExpiration = TimeSpan.FromMinutes(20); // 默认缓存时间
options.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(30); // 过时自动删除
/* 建表语句如下:
CREATE TABLE [dbo].[TestCache](
[Id] [nvarchar](449) NOT NULL,
[Value] [varbinary](max) NOT NULL,
[ExpiresAtTime] [datetimeoffset](7) NOT NULL,
[SlidingExpirationInSeconds] [bigint] NULL,
[AbsoluteExpiration] [datetimeoffset](7) NULL,
CONSTRAINT [PK_TestCache] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY])
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]*/
});
#elif MySql
builder.Services.AddDistributedMySqlCache(options => // 2.5 分布式缓存- MySql (Pomelo.Extensions.Caching.MySql)
{
options.ConnectionString = "server=127.0.0.1;uid=root;pwd=Aa123456;database=mysql;port=3306;pooling=false;SslMode=None;old Guids=true;Charset=utf8;AllowUserVariables=true;"; //AllowUserVariables必须加不然会报错
options.SchemaName = "TestDB"; // 数据库名称
options.TableName = "testcache"; // 表名
options.DefaultSlidingExpiration = TimeSpan.FromMinutes(20); // 默认缓存时间
options.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(30); // 过时自动删除
/* 建表语句如下:
CREATE TABLE `testcache` (
`Id` varchar(449) CHARACTER SET utf8 NOT NULL,
`Value` longblob NOT NULL,
`ExpiresAtTime` datetime NOT NULL,
`SlidingExpirationInSeconds` bigint(20) DEFAULT NULL,
`AbsoluteExpiration` datetime DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='缓存存储表'*/
});
#elif PostgreSql
builder.Services.AddDistributedPostgreSqlCache(options => // 2.6 分布式缓存- PostgreSql (Community.Microsoft.Extensions.Caching.PostgreSql)
{
options.ConnectionString = "host=127.0.0.1;Port=5432;database=testdb;username=root;password=Aa123456;"; // HOST=localhost;PORT=5432;DATABASE=testdb;USER ID=root;PASSWORD=Aa123456;
options.SchemaName = "TestDB"; // 数据库名称
options.TableName = "testcache"; //表名
options.DefaultSlidingExpiration = TimeSpan.FromMinutes(20); // 默认过期时间
options.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(30); //过时自动删除
});
#endif
#endregion 分布式内存
builder.Services.AddOutputCache(); // 3、其他缓存-输出缓存中间件
builder.Services.AddResponseCaching(); // 4、其他缓存-响应缓存中间件
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//#if MemCached
// app.UseEnyimMemcached(); // 可以注释掉这里;因为默认已调用了app.UseDefaultMemcached();
//#endif
app.UseAuthorization();
app.UseOutputCache(); // 启用其他缓存-输出缓存
app.UseResponseCaching(); // 启用其他缓存-响应缓存
app.MapControllers();
app.Run();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/qq28069933146_admin/csharp_distributedcache_simple.git
git@gitee.com:qq28069933146_admin/csharp_distributedcache_simple.git
qq28069933146_admin
csharp_distributedcache_simple
CSharp_DistributedCache_Simple
master

搜索帮助