資源簡介
/// <summary>
/// redis客戶端連接池信息
/// </summary>
private PooledRedisClientManager prcm;
public Redis()
{
CreateManager();
}
/// <summary>
/// 創建鏈接池管理對象
/// </summary>
private void CreateManager()
{
try
{
// ip1:端口1,ip2:端口2
var serverlist = ConfigurationManager.AppSettings["RedisServer"].Split(',');
prcm = new PooledRedisClientManager(serverlist, serverlist,
new RedisClientManagerConfig
{
MaxWritePoolSize = 32,
MaxReadPoolSize = 32,
AutoStart = true
});
// prcm.Start();
}
catch (Exception e)
{
#if DEBUG
throw;
#endif
}
}
/// <summary>
/// 客戶端緩存操作對象
/// </summary>
public IRedisClient GetClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient();
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(string key)
{
using (var client = prcm.GetClient())
{
return client.Remove(key);
}
}
/// <summary>
/// 獲取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object Get(string key)
{
using (var client = prcm.GetClient())
{
var bytes = client.Get<byte[]>(key);
var obj = new ObjectSerializer().Deserialize(bytes);
return obj;
}
}
/// <summary>
/// 獲取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetT<T>(string key) where T : class
{
//return Get(key) as T;
using (var client = prcm.GetClient())
{
return client.Get<T>(key);
}
}
/// <summary>
/// 獲取到值到內存中,在刪除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetWithDelete(string key)
{
var result = Get(key);
if (result != null)
Remove(key);
return result;
}
/// <summary>
/// 獲取到值到內存中,在刪除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetWithDelete<T>(string key) where T : class
{
var result = GetT<T>(key);
if (result != null)
Remove(key);
return result;
}
/// <summary>
/// 寫
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Set(string key, object value)
{
using (var client = prcm.GetClient())
{
if (client.ContainsKey(key))
{
return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value));
}
else
{
return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value));
}
}
}
/// <summary>
/// 寫
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public bool Set(string key, object value, DateTime expireTime)
{
using (var client = prcm.GetClient())
{
if (client.ContainsKey(key))
{
return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
}
else
{
return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
}
}
}
/// <summary>
/// 寫
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expire"></param>
/// <returns></returns>
public bool SetT<T>(string key, T value, DateTime expire) where T : class
{
try
{
using (var client = prcm.GetClient())
{
return client.Set<T>(key, value, expire);
}
}
catch
{
return false;
}
}
/// <summary>
/// 寫
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetT<T>(string key, T value) where T : class
{
try
{
using (var client = prcm.GetClient())
{
return client.Set<T>(key, value);
}
}
catch
{
return false;
}
}
public void Dispose()
{
Close();
}
public void Close()
{
var client = prcm.GetClient();
prcm.Dispose();
}
/// redis客戶端連接池信息
/// </summary>
private PooledRedisClientManager prcm;
public Redis()
{
CreateManager();
}
/// <summary>
/// 創建鏈接池管理對象
/// </summary>
private void CreateManager()
{
try
{
// ip1:端口1,ip2:端口2
var serverlist = ConfigurationManager.AppSettings["RedisServer"].Split(',');
prcm = new PooledRedisClientManager(serverlist, serverlist,
new RedisClientManagerConfig
{
MaxWritePoolSize = 32,
MaxReadPoolSize = 32,
AutoStart = true
});
// prcm.Start();
}
catch (Exception e)
{
#if DEBUG
throw;
#endif
}
}
/// <summary>
/// 客戶端緩存操作對象
/// </summary>
public IRedisClient GetClient()
{
if (prcm == null)
CreateManager();
return prcm.GetClient();
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(string key)
{
using (var client = prcm.GetClient())
{
return client.Remove(key);
}
}
/// <summary>
/// 獲取
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object Get(string key)
{
using (var client = prcm.GetClient())
{
var bytes = client.Get<byte[]>(key);
var obj = new ObjectSerializer().Deserialize(bytes);
return obj;
}
}
/// <summary>
/// 獲取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetT<T>(string key) where T : class
{
//return Get(key) as T;
using (var client = prcm.GetClient())
{
return client.Get<T>(key);
}
}
/// <summary>
/// 獲取到值到內存中,在刪除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetWithDelete(string key)
{
var result = Get(key);
if (result != null)
Remove(key);
return result;
}
/// <summary>
/// 獲取到值到內存中,在刪除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetWithDelete<T>(string key) where T : class
{
var result = GetT<T>(key);
if (result != null)
Remove(key);
return result;
}
/// <summary>
/// 寫
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool Set(string key, object value)
{
using (var client = prcm.GetClient())
{
if (client.ContainsKey(key))
{
return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value));
}
else
{
return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value));
}
}
}
/// <summary>
/// 寫
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expireTime"></param>
/// <returns></returns>
public bool Set(string key, object value, DateTime expireTime)
{
using (var client = prcm.GetClient())
{
if (client.ContainsKey(key))
{
return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
}
else
{
return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
}
}
}
/// <summary>
/// 寫
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expire"></param>
/// <returns></returns>
public bool SetT<T>(string key, T value, DateTime expire) where T : class
{
try
{
using (var client = prcm.GetClient())
{
return client.Set<T>(key, value, expire);
}
}
catch
{
return false;
}
}
/// <summary>
/// 寫
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool SetT<T>(string key, T value) where T : class
{
try
{
using (var client = prcm.GetClient())
{
return client.Set<T>(key, value);
}
}
catch
{
return false;
}
}
public void Dispose()
{
Close();
}
public void Close()
{
var client = prcm.GetClient();
prcm.Dispose();
}
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?ClassLibrary2
{
????public?static?class?Cache
????{
????????private?static?object?cacheLocker?=?new?object();//緩存鎖對象
????????private?static?ICache?cache?=?null;//緩存接口
????????static?Cache()
????????{
????????????Load();
????????}
????????///?
????????///?加載緩存
????????///?
????????///?
????????private?static?void?Load()
????????{
????????????try
????????????{
????????????????cache?=?new?Redis();
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????//Log.Error(ex.Message);
????????????}
????????}
????????public?static?ICache?GetCache()
????????{
????????????return?cache;
????????}
????????///?
????????///?獲得指定鍵的緩存值
????????///?
????????///?緩存鍵
????????///?緩存值
????????public?static?object?Get(string?key)
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-07-04?17:49??WebApplication1\
?????目錄???????????0??2017-07-04?14:34??WebApplication1\ClassLibrary2\
?????文件????????5425??2017-07-04?17:15??WebApplication1\ClassLibrary2\Cache.cs
?????文件????????3371??2017-07-04?16:34??WebApplication1\ClassLibrary2\ClassLibrary2.csproj
?????文件?????????639??2017-07-04?17:13??WebApplication1\ClassLibrary2\ICache.cs
?????目錄???????????0??2017-07-04?12:51??WebApplication1\ClassLibrary2\Properties\
?????文件????????1358??2017-07-04?12:51??WebApplication1\ClassLibrary2\Properties\AssemblyInfo.cs
?????文件????????6456??2017-07-04?17:13??WebApplication1\ClassLibrary2\Redis.cs
?????目錄???????????0??2017-07-04?12:53??WebApplication1\ClassLibrary2\bin\
?????目錄???????????0??2017-07-04?17:49??WebApplication1\ClassLibrary2\bin\Debug\
?????文件????????8192??2017-07-04?17:49??WebApplication1\ClassLibrary2\bin\Debug\ClassLibrary2.dll
?????文件???????24064??2017-07-04?17:49??WebApplication1\ClassLibrary2\bin\Debug\ClassLibrary2.pdb
?????文件??????335872??2015-03-31?18:09??WebApplication1\ClassLibrary2\bin\Debug\NServiceKit.Common.dll
?????文件??????105472??2015-03-31?18:09??WebApplication1\ClassLibrary2\bin\Debug\NServiceKit.Interfaces.dll
?????文件??????229376??2016-02-29?17:30??WebApplication1\ClassLibrary2\bin\Debug\NServiceKit.Redis.dll
?????文件??????195072??2014-04-17?15:20??WebApplication1\ClassLibrary2\bin\Debug\NServiceKit.Text.dll
?????目錄???????????0??2017-07-04?17:49??WebApplication1\ClassLibrary2\bin\Release\
?????目錄???????????0??2017-07-04?12:51??WebApplication1\ClassLibrary2\obj\
?????目錄???????????0??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\
?????文件?????????589??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\ClassLibrary2.csproj.FileListAbsolute.txt
?????文件????????8192??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\ClassLibrary2.dll
?????文件???????24064??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\ClassLibrary2.pdb
?????文件????????6046??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????目錄???????????0??2017-07-04?17:49??WebApplication1\ClassLibrary2\obj\Debug\TempPE\
?????目錄???????????0??2017-07-04?15:23??WebApplication1\WebApplication1\
?????目錄???????????0??2017-07-04?17:49??WebApplication1\WebApplication1\App_Data\
?????文件??????????99??2017-07-04?12:09??WebApplication1\WebApplication1\Global.asax
?????文件????????1208??2017-07-04?12:09??WebApplication1\WebApplication1\Global.asax.cs
?????目錄???????????0??2017-07-04?12:09??WebApplication1\WebApplication1\Properties\
?????文件????????1331??2017-07-04?12:09??WebApplication1\WebApplication1\Properties\AssemblyInfo.cs
?????文件????????1223??2017-07-04?12:09??WebApplication1\WebApplication1\Web.Debug.config
............此處省略32個文件信息
評論
共有 條評論