代码实例

namespace RedisTest
{
class Program
{
static void Main(string[] args)
{
Student stu = RedisOperator.Instance.Get<Student>("stu");
RedisOperator.Instance.Set<Student>("stu", new Student { Id = , Name = "bb" }, );
Student stu1 = RedisOperator.Instance.Get<Student>("stu");
Console.ReadKey();
}
}
class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
}

控制台 program

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace RedisTest
{
/// <summary>
/// Install-Package ServiceStack.Redis
/// </summary>
public class RedisManager
{
/// <summary>
/// RedisManager类主要是创建链接池管理对象的
/// </summary>
private static string RedisPath = System.Configuration.ConfigurationManager.AppSettings["RedisPath"];
private static PooledRedisClientManager _prcm; /// <summary>
/// 静态构造方法,初始化链接池管理对象
/// </summary>
static RedisManager()
{
CreateManager();
} /// <summary>
/// 创建链接池管理对象
/// </summary>
private static void CreateManager()
{
_prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
} private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
//WriteServerList:可写的Redis链接地址。
//ReadServerList:可读的Redis链接地址。
//MaxWritePoolSize:最大写链接数。
//MaxReadPoolSize:最大读链接数。
//AutoStart:自动重启。
//LocalCacheTime:本地缓存到期时间,单位:秒。
//RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。
//RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应 // 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = , // “写”链接池链接数
MaxReadPoolSize = , // “读”链接池链接数
AutoStart = true,
});
} private static IEnumerable<string> SplitString(string strSource, string split)
{
return strSource.Split(split.ToArray());
} /// <summary>
/// 客户端缓存操作对象
/// </summary>
public static IRedisClient GetClient()
{
if (_prcm == null)
{
CreateManager();
}
return _prcm.GetClient();
}
} }

RedisManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using ServiceStack.Redis.Support; namespace RedisTest
{
public class RedisOperator:IDisposable
{
private IRedisClient _redis;
private int _defaultSessionMinute = ;
public RedisOperator()
{
this._redis = RedisManager.GetClient();
string RedisSessionTimeOutMinute = System.Configuration.ConfigurationManager.AppSettings["RedisSessionTimeOutMinute"];
if (!string.IsNullOrEmpty(RedisSessionTimeOutMinute) && !int.TryParse(RedisSessionTimeOutMinute, out this._defaultSessionMinute))
{
this._defaultSessionMinute = ;
}
}
private static RedisOperator _instance;
private static object lockobj = new object();
public static RedisOperator Instance {
get
{
if (_instance == null)
{
lock (lockobj)
{
if (_instance == null)
{
_instance = new RedisOperator();
}
}
}
return _instance;
} } public void Set<T>(string key, T obj, double minuteOut = )
{
if (minuteOut >= )
{
if (minuteOut == )
minuteOut = this._defaultSessionMinute;
this._redis.Set<T>(key, obj, TimeSpan.FromMinutes(minuteOut));
}
else
{
this._redis.Set<T>(key, obj);
}
} public T Get<T>(string key)
{
return this._redis.Get<T>(key);
} public bool Remove(string key)
{
return this._redis.Remove(key);
} public void Dispose()
{
try
{
if (this._redis != null)
this._redis.Dispose();
}
catch (Exception ex)
{ }
}
}
}

RedisOperator

webConfig

<appSettings>
<!--Redis 配置 -->
<add key="RedisPath" value="127.0.0.1:6379" />
<add key="RedisSessionTimeOutMinute" value="20" />
</appSettings>

1.Redis Manager 用于处理Reids 连接池

2.RedisOperator 负责Redis缓存的添加,删除

3.配置文件 RedisPath  Redis服务器地址,端口用默认的6379

4.RedisSessionTimeOutMinute  缓存过期时间可以通过配置默认20分钟 代码里边也可以传值设置时间的话就不走配置的默认值了(代码RedisOperator里有判断)

注意:1.由于redis的安全设置,只允许本地访问,

2.若要让其他iP 可访问,那么

a.在安装的redis 文件夹下找到redis.conf 文件 将 所有的bind 127.0.0.1   bind..  注释掉

b.在电脑防火墙  入站规则里边添加新建规则 redis   tcp 端口为 6379

分布式缓存 Redis(二)的更多相关文章

  1. .NET分布式缓存Redis从入门到实战

    一.课程介绍 今天阿笨给大家带来一堂NOSQL的课程,本期的主角是Redis.希望大家学完本次分享课程后对redis有一个基本的了解和认识,并且熟悉和掌握 Redis在.NET中的使用. 本次分享课程 ...

  2. 三点须知:当我们在开发过程中需要用到分布式缓存Redis的时候

    当我们在开发过程中需要用到分布式缓存Redis的时候,我们首先要明白缓存在系统中用来做什么? 1. 少量数据存储,高速读写访问.通过数据全部in-momery 的方式来保证高速访问,同时提供数据落地的 ...

  3. ASP.Net Core使用分布式缓存Redis从入门到实战演练

    一.课程介绍 人生苦短,我用.NET Core!缓存在很多情况下需要用到,合理利用缓存可以一方面可以提高程序的响应速度,同时可以减少对特定资源访问的压力.  所以经常要用到且不会频繁改变且被用户共享的 ...

  4. 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

  5. 一个技术汪的开源梦 —— 公共组件缓存之分布式缓存 Redis 实现篇

    Redis 安装 & 配置 本测试环境将在 CentOS 7 x64 上安装最新版本的 Redis. 1. 运行以下命令安装 Redis $ wget http://download.redi ...

  6. 企业项目开发--分布式缓存Redis

    第九章 企业项目开发--分布式缓存Redis(1) 注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis ...

  7. 第十章 企业项目开发--分布式缓存Redis(2)

    注意:本章代码是在上一章的基础上进行添加修改,上一章链接<第九章 企业项目开发--分布式缓存Redis(1)> 上一章说了ShardedJedisPool的创建过程,以及redis五种数据 ...

  8. 分布式缓存Redis应用场景解析

    Redis的应用场景非常广泛.虽然Redis是一个key-value的内存数据库,但在实际场景中,Redis经常被作为缓存来使用,如面对数据高并发的读写.海量数据的读写等. 举个例子,A网站首页一天有 ...

  9. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍

    笔记 1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具          1.redis官网 https://redis.io/download   ...

随机推荐

  1. idea整个项目乱码解决办法

    idea前几天还好好的,这两天一直出现乱码.不止一个文件,是整个项目都有这个问题.想想这几天什么也没做啊... 上网搜了下 解决. 经过多次排查寻找,终于,在项目的目录下有个.idea的文件夹,这个文 ...

  2. 微软智能云的核心DNA

    你知道吗? 今天,微软智能云平台运行于全球30个区域,体量超过了两大云服务商亚马逊和谷歌的总和: 今天,在全球范围内超过85%的财富五百强企业都使用了微软Azure云服务,而中国部分的用户也达到了六万 ...

  3. NS Simulation: Scheduling Events (examples inside)

    NS Simulation: Scheduling Events Simulation time A similation system (such as NS) must have a built- ...

  4. 使用UIDynamicAnimator创建重力感应的View

    使用UIDynamicAnimator创建重力感应的View http://www.raywenderlich.com/zh-hans/52617/uikit-力学教程 详细教程请参考上面的链接,此处 ...

  5. zimbra邮件服务器的搭建和迁移

    背景: 公司最近由于服务器费用问题,需要将邮件服务器从亚马逊(新加坡)云服务器A迁移到阿里云(香港)云服务器B. 由于邮箱使用的是域名访问,但是没有进行备案,所以只能迁移到港澳台地区,才能正常使用. ...

  6. QT写hello world 以及信号槽机制

    QT是一个C++的库,不仅仅有GUI的库.首先写一个hello world吧.敲代码,从hello world 写起. #include<QtGui/QApplication> #incl ...

  7. 新款 2018款macbook Pro 装双系统教程

    首个阅读量将破万的文章,感谢支持.防止无良爬虫,开头附上原文链接:http://www.cnblogs.com/xueyudlut/p/7498115.html ------------------- ...

  8. linux 上部署tomcat的java web项目,会出现post提交request.request.getParameter()的得不到值的情况

    有时候明明在windows上非常的正常,而在linux上就不正常了,在windows上post提交request.request.getParameter()有值,而在liunx上没有值. 我开始以为 ...

  9. Python深入学习之《Fluent Python》 Part 1

    Python深入学习之<Fluent Python> Part 1 从上个周末开始看这本<流畅的蟒蛇>,技术是慢慢积累的,Python也是慢慢才能写得优雅(pythonic)的 ...

  10. redis在windows平台安装和启动

    官网: https://redis.io/ 中文网站:http://www.redis.net.cn/ 一.下载windows版本的redis 官网没有提供windows版本的下载,只有linux版本 ...