资源简介
/// <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个文件信息
相关资源
- C#联通网络宽带测试 拨号
- C#百度指数抓取方法(2012年版本已失
- C# 隐藏某个磁盘分区
- wince引脚控制程序
- C# 读取并编辑window系统的右键菜单
- C#自定义屏保(不断滚动的文字)
- C#winform打印指定区域 -控件拖动 -设置
- C#使用Hook进行改键
- 提供C#调用系统API函数弹出或收起光驱
- 通过C#自带的头文件(类)获取Windo
- C#获取电脑CPU以及内存使用率
- Syndication实现读取、创建、订阅、更新
- 利用uu云打码平台的lib实现的c#打码平
- tf-idf一种计算方法
- C# pop3 邮件接收程序
- C# 邮件群发示例 源码下载18952
- httpclient source code by csharp
- 动态抓取IPC#实现
- XXTEA算法的C#实现和JS实现,可以互相
- C# 飞行棋 游戏源码(面向对象入门)
- Socke传输 (wince6.0系统)
- UDP Messenger 1.0.unitypackage
- asp.net 网页静态化组件(shipingx-Stati
- SocketAsyncEventArgs完成断开编程
- 基于WinPcap的C# ARP欺骗软件().rar
- C#网络应用编程 矩阵并行计算练习
- 猜数小游戏WCF网络编程技术(附服务
- 《C#版Ftp软件源码》
- 基于com串口的文件发送和接收
-
C# 播放铃声(AxWindowsMediaPla
yer)最新
评论
共有 条评论