(#3)增加Redis数据库存取方法

This commit is contained in:
falcon 2019-03-04 13:53:35 +08:00
parent 105549e323
commit 7ad681c79a
5 changed files with 138 additions and 7 deletions

View File

@ -0,0 +1,85 @@
using System;
using Newtonsoft.Json;
using StackExchange.Redis;
namespace Cmdjy.Bll
{
public interface ICache
{
/// <summary>
/// 是否可用
/// </summary>
bool Usable { get; }
/// <summary>
/// 获取数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="key">保存的key</param>
/// <returns>缓存的数据</returns>
T GetData<T>(string key);
/// <summary>
/// 缓存数据,缓存时间默认
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="key">保存的key</param>
/// <param name="obj">缓存的数据</param>
void SetData<T>(string key,T obj);
/// <summary>
/// 缓存数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="key">保存的key</param>
/// <param name="obj">缓存的数据</param>
/// <param name="timeSpan">缓存时间</param>
void SetData<T>(string key,T obj,TimeSpan timeSpan);
}
/// <summary>
/// 数据缓存实现类
/// </summary>
public class RedisHelper:ICache
{
public IDatabase Database { get; set; }
public static ConnectionMultiplexer Connection { get; set; }
public bool Usable {
get {
return Connection != null && Connection.IsConnected && Database != null;
}
}
public RedisHelper() {
if(Connection == null) {
var connectionStr = WebSettings.RedisConnectionString;
Connection = ConnectionMultiplexer.Connect(connectionStr);
}
this.Database = this.Database ?? Connection.GetDatabase(0);
}
public T GetData<T>(string key) {
if(!Usable) return default(T);
return fromJson<T>(Database.StringGet(key));
}
public void SetData<T>(string key,T obj) {
SetData(key,obj,new TimeSpan(3,0,0,0));
}
public void SetData<T>(string key,T obj,TimeSpan timeSpan) {
if(!Usable) return;
if(timeSpan == null) {
this.Database.StringSet(key,toJson(obj));
}
else {
this.Database.StringSet(key,toJson(obj),timeSpan);
}
}
private static string toJson(object value) {
return JsonConvert.SerializeObject(value);
}
private static T fromJson<T>(string value) {
return JsonConvert.DeserializeObject<T>(value);
}
}
}

View File

@ -55,9 +55,40 @@
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Pipelines.Sockets.Unofficial, Version=1.0.0.0, Culture=neutral, PublicKeyToken=42ea0a778e13fbe2, processorArchitecture=MSIL">
<HintPath>..\packages\Pipelines.Sockets.Unofficial.1.0.7\lib\net461\Pipelines.Sockets.Unofficial.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.2.0.519\lib\net461\StackExchange.Redis.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Diagnostics.PerformanceCounter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.PerformanceCounter.4.5.0\lib\net461\System.Diagnostics.PerformanceCounter.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Pipelines, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Pipelines.4.5.1\lib\netstandard2.0\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Channels, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Channels.4.5.0\lib\netstandard2.0\System.Threading.Channels.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
@ -154,6 +185,7 @@
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Bll\DesHelper.cs" />
<Compile Include="Bll\Cache.cs" />
<Compile Include="Dal\Wappers\HisDrugInfoWapper.cs" />
<Compile Include="Dal\Wappers\HisPrescriptyInfoWapper.cs" />
<Compile Include="Controllers\HomeController.cs" />

View File

@ -16,12 +16,12 @@
<!--双方协商需要8位长度超过8位取8位小于8位抛出异常-->
<add key="DesKey" value="wsddjy19" />
<!--Redis服务器连接字符串,localhost:13919,password=123-->
<add key="RedisConnectionString" value=""/>
</appSettings>
<connectionStrings>
<add name="DjyDbContext"
connectionString="Data Source=FALCON-PC\SQLSERVER2008R2;Persist Security info=True;Initial Catalog=cmdjy;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="DjyDbContext" connectionString="Data Source=FALCON-PC\SQLSERVER2008R2;Persist Security info=True;Initial Catalog=cmdjy;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />

View File

@ -11,8 +11,6 @@ namespace Cmdjy
/// <summary>
/// 获取配置的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetValue(String key) {
var val = ConfigurationManager.AppSettings[key];
return val;
@ -23,5 +21,11 @@ namespace Cmdjy
public static string DesKey {
get => GetValue("DesKey") ?? throw new Exception("必须在web.config文件appSettings节配置DesKey的值");
}
/// <summary>
/// 获取Redis服务器连接字符串
/// </summary>
public static string RedisConnectionString {
get => GetValue("RedisConnectionString");
}
}
}

View File

@ -26,6 +26,16 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.8.3" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
<package id="Pipelines.Sockets.Unofficial" version="1.0.7" targetFramework="net461" />
<package id="StackExchange.Redis" version="2.0.519" targetFramework="net461" />
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
<package id="System.Diagnostics.PerformanceCounter" version="4.5.0" targetFramework="net461" />
<package id="System.IO.Pipelines" version="4.5.1" targetFramework="net461" />
<package id="System.Memory" version="4.5.1" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net461" />
<package id="System.Threading.Channels" version="4.5.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net461" />
<package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>