1.使用Microsoft.Extensions.Caching.Memory插件,缓存数据
#region 使用Microsoft.Extensions.Caching.Memory插件,缓存数据
{
//创建数据
string data = "hello";
string data_key = "data_key";
//创建缓存接口
MemoryCacheOptions memoryCacheOptions = new MemoryCacheOptions();
IMemoryCache memoryCache = new MemoryCache(memoryCacheOptions);
//存储数据
MemoryCacheEntryOptions memoryCacheEntryOptions = new MemoryCacheEntryOptions();
// memoryCacheEntryOptions.SetAbsoluteExpiration(TimeSpan.FromSeconds(5));//绝对过期时间
memoryCacheEntryOptions.SetSlidingExpiration(TimeSpan.FromSeconds(5)); //滑动过期时间
memoryCache.Set<string>(data_key, data, memoryCacheEntryOptions);
//获取数据【过期时间之内】
string curCache = memoryCache.Get<string>(data_key);
Console.WriteLine($"curCache:{curCache}");
//获取数据【过期时间之外】
Thread.Sleep(6000);
curCache = memoryCache.Get<string>(data_key);
Console.WriteLine($"curCache:{curCache}");
Console.ReadKey();
}
#endregion
2.使用Microsoft.Extensions.Caching.StackExchangeRedis插件,缓存数据
#region 使用Microsoft.Extensions.Caching.StackExchangeRedis插件,缓存数据
{
//创建数据
string data = "hello";
string data_key = "data_key";
//创建缓存接口
RedisCacheOptions redisCacheOptions = new RedisCacheOptions();
redisCacheOptions.Configuration = "localhost:6379";
IDistributedCache distributedCache = new RedisCache(redisCacheOptions);
//存储数据
distributedCache.SetString(data_key, data, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5) });
//获取数据【过期时间之内】
string curCache = distributedCache.GetString(data_key);
Console.WriteLine($"curCache:{curCache}");
//获取数据【过期时间之外】
Thread.Sleep(6000);
curCache = distributedCache.GetString(data_key);
Console.WriteLine($"curCache:{curCache}");
Console.ReadKey();
}
#endregion
3.使用自定义字典缓存数据
(1)创建自定义字典类
public class CustomCache : IDistributedCache
{
private ConcurrentDictionary<string, byte[]> cache = new ConcurrentDictionary<string, byte[]>();
public byte[]? Get(string key)
{
cache.TryGetValue(key, out var value);
return value;
}
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default)
{
return Get(key);
}
public void Refresh(string key)
{
}
public async Task RefreshAsync(string key, CancellationToken token = default)
{
Refresh(key);
}
public void Remove(string key)
{
cache.TryRemove(key, out var value);
}
public async Task RemoveAsync(string key, CancellationToken token = default)
{
Remove(key);
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
cache[key] = value;
}
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
Set(key, value, options);
}
}
(2)主要代码
#region 使用自定义-字典缓存数据
{
//创建数据
string data = "hello";
string data_key = "data_key";
//创建缓存接口
IDistributedCache distributedCache = new CustomCache();
//存储数据
byte[] bytes = Encoding.UTF8.GetBytes(data);
distributedCache.Set(data_key, bytes);
byte[] curBytes = distributedCache.Get(data_key);
string dataStr = Encoding.UTF8.GetString(curBytes);
Console.WriteLine($"curCache:{dataStr}");
Console.ReadKey();
}
#endregion
4.使用自定义Mysql缓存数据,需要安装MySqlConnector插件
(1)创建Mysql自定义类
public class MysqlCustomCacheOptions
{
public string MysqlAddress { get; set; }
}
public class MysqlCustomCache : IDistributedCache
{
private readonly MysqlCustomCacheOptions mysqlCustomCacheOptions;
public MysqlCustomCache(MysqlCustomCacheOptions mysqlCustomCacheOptions)
{
this.mysqlCustomCacheOptions = mysqlCustomCacheOptions;
}
public byte[]? Get(string key)
{
//获取Mysql中缓存数据
using (var connection = new MySqlConnection(mysqlCustomCacheOptions.MysqlAddress))
{
var command = connection.CreateCommand();
command.CommandText = "select * from MysqlCache where CacheKey=@CacheKey";
command.Parameters.AddWithValue("@CacheKey", key);
connection.Open();
var reader = command.ExecuteReader();
var dt = reader.GetSchemaTable();
return Encoding.UTF8.GetBytes(dt.Rows[0][0]?.ToString());
}
}
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default)
{
return Get(key);
}
public void Refresh(string key)
{
}
public async Task RefreshAsync(string key, CancellationToken token = default)
{
Refresh(key);
}
public void Remove(string key)
{
}
public async Task RemoveAsync(string key, CancellationToken token = default)
{
Remove(key);
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
using (var connection = new MySqlConnection(mysqlCustomCacheOptions.MysqlAddress))
{
var command = connection.CreateCommand();
command.CommandText = "INSERT INTO MysqlCache (CacheKey,CacheValue) VALUES (@CacheKey,@CacheValue)";
command.Parameters.AddWithValue("@CacheKey", key);
command.Parameters.AddWithValue("@CacheValue", Encoding.UTF8.GetString(value));
connection.Open();
command.ExecuteNonQuery();
}
}
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
Set(key, value, options);
}
}
(2)需要在mysql数据库中创建对应的表
(3)主要代码
#region 使用自定义-Mysql缓存数据
{
//创建数据
string data = "hello";
string data_key = "data_key";
//创建缓存接口
MysqlCustomCacheOptions mySQLCustomCacheOptions = new MysqlCustomCacheOptions();
mySQLCustomCacheOptions.MysqlAddress = "server=localhost;database=Net8Test;user=root;password=123456;";
IDistributedCache distributedCache = new MysqlCustomCache(mySQLCustomCacheOptions);
//存储数据
byte[] bytes = Encoding.UTF8.GetBytes(data);
distributedCache.Set(data_key, bytes);
byte[] curBytes = distributedCache.Get(data_key);
string dataStr = Encoding.UTF8.GetString(curBytes);
Console.WriteLine($"curCache:{dataStr}");
Console.ReadKey();
}
#endregion