12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using StackExchange.Redis;
- using System.Configuration;
- using Newtonsoft.Json;
- namespace Cache
- {
- public class RedisService:ICache
- {
- private static readonly string Coonstr = ConfigurationManager.ConnectionStrings["redisconnstring"].ToString();
- private static object _locker = new Object();
- private static ConnectionMultiplexer _instance = null;
-
-
-
- public ConnectionMultiplexer Instance
- {
- get
- {
- if (_instance == null)
- {
- lock (_locker)
- {
- if (_instance == null || !_instance.IsConnected)
- {
- _instance = ConnectionMultiplexer.Connect(Coonstr);
- }
- }
- }
-
-
-
-
-
-
-
- return _instance;
- }
- }
- static RedisService()
- {
-
- }
- public void DeleteTable(string table)
- {
- var db = _instance.GetDatabase();
- db.KeyDelete(table);
- }
- public void Insert(string table, Object data)
- {
- var db = _instance.GetDatabase();
-
- db.StringSet(table, JsonConvert.SerializeObject(data));
- }
- public T Get<T>(string table)
- {
- var db = _instance.GetDatabase();
- var data=db.StringGet(table);
- return JsonConvert.DeserializeObject<T>(data);
- }
-
- }
- }
|