-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataCacheHelper.cs
56 lines (51 loc) · 1.83 KB
/
DataCacheHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Com.Ddlev.DataCache
{
public class DataCacheHelper
{
static readonly object IClock = new object();
static Dictionary<string, IDataCacheHelper> dic = new Dictionary<string, IDataCacheHelper>();
/// <summary>
///
/// </summary>
/// <param name="ct"></param>
/// <param name="_dbRadis">只对Radis数据库有效</param>
/// <param name="_conn">连接地址和端口,只对Radis数据库有效</param>
/// <returns></returns>
public static IDataCacheHelper GetCacheHelp(DataCacheType ct,int _dbRadis=-1,string conn=null)
{
var _conn = ct== DataCacheType.Redis?( conn ?? ConfigurationManager.AppSettings["RedisConfig"] ?? "127.0.0.1:6379"):"";
var key = ct.ToString() + "db" + _conn;
lock (IClock)
{
if (dic.Count==0 || !dic.ContainsKey(key) || dic[key]==null)
{
IDataCacheHelper _IDataCacheHelper = null;
switch (ct)
{
case DataCacheType.Redis:
_IDataCacheHelper = Redis.RedisPool.GetPool(_conn, _dbRadis);
dic.Add(key, _IDataCacheHelper);
break;
case DataCacheType.Sysnet:
_IDataCacheHelper = Sysnet.SysnetPool.GetPool();
dic.Add(key, _IDataCacheHelper);
break;
}
}
return dic[key];
}
}
}
public enum DataCacheType
{
Sysnet=0,
Redis=1
}
}