132 lines
4.2 KiB
C#
132 lines
4.2 KiB
C#
using StackExchange.Redis;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using WX.CRM.Model.Redis;
|
|
|
|
namespace WX.CRM.DAL.Redis
|
|
{
|
|
public abstract class RedisStore
|
|
{
|
|
public RedisKey Key { get; set; }
|
|
public RedisSettings Settings { get; private set; }
|
|
|
|
//static ConcurrentDictionary<RedisConfig, RedisSettings> _conn = new ConcurrentDictionary<RedisConfig, RedisSettings>();
|
|
|
|
internal RedisStore(RedisConfig redisConfig = RedisConfig.Redis0)
|
|
{
|
|
//var config = RedisConfigurationSection.GetSection().RedisSettings[Enum.GetName(typeof(RedisConfig), redisConfig)];
|
|
//if (config == null)
|
|
// throw new Exception("redis config err");
|
|
//LogHelper.Info("请求hashCode:" + redisConfig.GetHashCode().ToString());
|
|
|
|
//if (!_conn.ContainsKey(redisConfig))
|
|
//{
|
|
// _conn.GetOrAdd(redisConfig, new RedisSettings(config.ConnectionString, config.Db));
|
|
// WX.CRM.Common.LogHelper.Info("RedisStore(RedisConfig redisConfig = RedisConfig.Redis0):" + string.Join(",", _conn.Keys));
|
|
// WX.CRM.Common.LogHelper.Info("存在:" + string.Join(",", _conn.Keys.GetHashCode()));
|
|
//}
|
|
//this.Settings = _conn[redisConfig];
|
|
this.Settings = RedisSettings.GetRedisSettings(redisConfig);
|
|
//this.Settings = new RedisSettings(config.ConnectionString, config.Db);
|
|
}
|
|
internal RedisStore(RedisKey key, RedisConfig redisConfig = RedisConfig.Redis0)
|
|
{
|
|
//var config = RedisConfigurationSection.GetSection().RedisSettings[Enum.GetName(typeof (RedisConfig), redisConfig)];
|
|
//if (config == null)
|
|
// throw new Exception("redis config err");
|
|
|
|
//if (!_conn.ContainsKey(redisConfig))
|
|
//{
|
|
// _conn.GetOrAdd(redisConfig, new RedisSettings(config.ConnectionString, config.Db));
|
|
|
|
|
|
// WX.CRM.Common.LogHelper.Info("RedisStore(RedisKey key, RedisConfig redisConfig = RedisConfig.Redis0):" + string.Join(",", _conn.Keys));
|
|
// foreach (var k in _conn.Keys)
|
|
// {
|
|
// LogHelper.Info("connkeys:" + k.GetHashCode());
|
|
// }
|
|
//}
|
|
//this.Settings = _conn[redisConfig];
|
|
this.Settings = RedisSettings.GetRedisSettings(redisConfig);
|
|
//this.Settings = new RedisSettings(config.ConnectionString, config.Db);
|
|
this.Key = key;
|
|
}
|
|
|
|
internal ConnectionMultiplexer Connection
|
|
{
|
|
get
|
|
{
|
|
return Settings.GetConnection();
|
|
}
|
|
}
|
|
|
|
internal IDatabase Database
|
|
{
|
|
get
|
|
{
|
|
return Connection.GetDatabase(Settings.Db);
|
|
}
|
|
}
|
|
|
|
public bool Expire(DateTime expire)
|
|
{
|
|
return Database.KeyExpire(Key, expire);
|
|
}
|
|
|
|
public bool Expire(TimeSpan expire)
|
|
{
|
|
return Database.KeyExpire(Key, expire);
|
|
}
|
|
|
|
public async Task<bool> ExpireAsync(DateTime expire)
|
|
{
|
|
return await Database.KeyExpireAsync(Key, expire);
|
|
}
|
|
|
|
public async Task<bool> ExpireAsync(TimeSpan expire)
|
|
{
|
|
return await Database.KeyExpireAsync(Key, expire);
|
|
}
|
|
|
|
public bool Exists()
|
|
{
|
|
return Exists(Key);
|
|
}
|
|
|
|
public bool Exists(RedisKey key)
|
|
{
|
|
return Database.KeyExists(key);
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync()
|
|
{
|
|
return await ExistsAsync(Key);
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(RedisKey key)
|
|
{
|
|
return await Database.KeyExistsAsync(key);
|
|
}
|
|
public bool Del()
|
|
{
|
|
return Database.KeyDelete(Key);
|
|
}
|
|
public bool Del(RedisKey key)
|
|
{
|
|
return Database.KeyDelete(key);
|
|
}
|
|
|
|
public async Task<bool> DelAsync()
|
|
{
|
|
return await Database.KeyDeleteAsync(Key);
|
|
}
|
|
|
|
public async Task<bool> DelAsync(RedisKey key)
|
|
{
|
|
Key = key;
|
|
return await Database.KeyDeleteAsync(Key);
|
|
}
|
|
}
|
|
|
|
}
|