缓存搭建

This commit is contained in:
橙子
2022-10-24 22:31:53 +08:00
parent 51c13c7b52
commit c29aeeee41
8 changed files with 153 additions and 113 deletions

View File

@@ -0,0 +1,78 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Yi.Framework.Common.IOCOptions;
using CSRedis;
namespace Yi.Framework.Core
{
public abstract class CacheInvoker
{
private readonly RedisConnOptions _RedisOptions;
protected CacheInvoker Client { get; set; }
public CacheInvoker Db { get { return Client; } set { } }
public CacheInvoker(IOptionsMonitor<RedisConnOptions> redisConnOptions)
{
}
public virtual bool Exit(string key)
{
throw new NotImplementedException();
}
public virtual long Remove(string key)
{
throw new NotImplementedException();
}
public virtual long HRemove(string key, params string[] par)
{
throw new NotImplementedException();
}
public virtual T Get<T>(string key)
{
throw new NotImplementedException();
}
public virtual bool Set<T>(string key, T data, TimeSpan time)
{
throw new NotImplementedException();
}
public virtual bool Set<T>(string key, T data)
{
throw new NotImplementedException();
}
public virtual T QueuePop<T>(string key)
{
throw new NotImplementedException();
}
public virtual long QueuePush<T>(string key, T data)
{
throw new NotImplementedException();
}
public virtual long QueueLen(string key)
{
throw new NotImplementedException();
}
public virtual bool HSet<T>(string key, string fieId, T data)
{
throw new NotImplementedException();
}
public virtual bool HSet<T>(string key, string fieId, T data, TimeSpan time)
{
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Common.IOCOptions;
namespace Yi.Framework.Core.Cache
{
public class MemoryCacheClient: CacheInvoker
{
private IMemoryCache _client;
public MemoryCacheClient(IOptionsMonitor<RedisConnOptions> redisConnOptions):base(redisConnOptions)
{
_client = new MemoryCache(new MemoryCacheOptions() { });
}
public override T Get<T>(string key)
{
return _client.Get<T>(key);
}
public override bool Set<T>(string key, T item)
{
return _client.Set(key, item) is not null;
}
public override bool Set<T>(string key, T item, TimeSpan time)
{
return _client.Set(key, item, time) is not null;
}
}
}

View File

@@ -0,0 +1,42 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Yi.Framework.Common.IOCOptions;
using CSRedis;
namespace Yi.Framework.Core
{
public class RedisClient: CacheInvoker
{
private readonly RedisConnOptions _RedisOptions;
private CSRedisClient _client;
public RedisClient(IOptionsMonitor<RedisConnOptions> redisConnOptions):base(redisConnOptions)
{
this._RedisOptions = redisConnOptions.CurrentValue;
_client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }");
}
public override T Get<T>(string key)
{
return _client.Get<T>(key);
}
public override bool Set<T>(string key, T data, TimeSpan time)
{
return _client.Set(key, data, time);
}
public override bool Set<T>(string key, T data)
{
return _client.Set(key, data);
}
}
}