Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.Core/CacheInvoker.cs

114 lines
3.1 KiB
C#
Raw Normal View History

2021-10-10 17:30:31 +08:00
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;
2021-10-10 17:52:23 +08:00
using Yi.Framework.Common.IOCOptions;
2022-04-02 17:44:50 +08:00
using CSRedis;
2021-10-10 17:30:31 +08:00
2021-10-10 17:52:23 +08:00
namespace Yi.Framework.Core
2021-10-10 17:30:31 +08:00
{
2022-10-17 01:10:31 +08:00
public class CacheInvoker
2022-04-02 17:44:50 +08:00
{
public delegate T MyAction<T>(CSRedisClient client);
private readonly RedisConnOptions _RedisOptions;
private CSRedisClient Client { get; set; }
public CSRedisClient _Db { get { return Client; } set { } }
2022-10-17 01:10:31 +08:00
public CacheInvoker(IOptionsMonitor<RedisConnOptions> redisConnOptions)
2022-04-02 17:44:50 +08:00
{
this._RedisOptions = redisConnOptions.CurrentValue;
Client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }");
2022-04-02 17:44:50 +08:00
}
2022-07-13 10:32:43 +08:00
2022-04-02 17:44:50 +08:00
private T TryCatch<T>(MyAction<T> action)
{
T result = default(T);
2022-04-02 17:44:50 +08:00
try
{
result = action(Client);
2022-04-02 17:44:50 +08:00
}
catch (Exception exinfo)
{
Console.WriteLine(exinfo);
}
//finally
//{
// Client.Dispose();
//}
2022-04-02 17:44:50 +08:00
return result;
}
public bool Exit(string key)
{
return this.TryCatch((u) => u.Exists(key));
}
public long Remove(string key)
{
return this.TryCatch((u) => u.Del(key));
}
2022-07-13 10:32:43 +08:00
public long HRemove(string key, params string[] par)
{
return this.TryCatch((u) => u.HDel(key, par));
}
2022-04-02 17:44:50 +08:00
public T Get<T>(string key)
{
return this.TryCatch<T>((u) => u.Get<T>(key));
}
public bool Set<T>(string key, T data, TimeSpan time)
{
return this.TryCatch<bool>((u) => u.Set(key, data, time));
}
public bool Set<T>(string key, T data)
{
return this.TryCatch<bool>((u) => u.Set(key, data));
}
2022-07-13 10:32:43 +08:00
public T QueuePop<T>(string key)
{
return this.TryCatch<T>((u) => u.RPop<T>(key));
}
public long QueuePush<T>(string key, T data)
{
return this.TryCatch<long>((u) => u.LPush<T>(key, data));
}
public long QueueLen(string key)
{
return TryCatch((u) => u.LLen(key));
}
public bool HSet<T>(string key, string fieId, T data)
{
return this.TryCatch<bool>((u) => u.HSet(key, fieId, data));
}
public bool HSet<T>(string key, string fieId, T data, TimeSpan time)
{
return this.TryCatch<bool>((u) =>
{
var res = u.HSet(key, fieId, data);
u.Expire(key, time);
return res;
});
}
public CSRedisClient Db()
2022-04-10 19:25:49 +08:00
{
2022-07-13 10:32:43 +08:00
return new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }");
2022-04-10 19:25:49 +08:00
}
2022-04-02 17:44:50 +08:00
}
2021-10-10 17:30:31 +08:00
}