代码实例

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. 创建第一个windows服务

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志. 计算机启动时,服务会自动开 ...

  2. 微信证书 javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

    这几天在做微信退款接口,按照api写完之后,在本地测试了下没有问题,于是交给测试让他们在测试环境开测.他们说退款没有成功,感觉去查日志,发现后台报了 javax.net.ssl.SSLExceptio ...

  3. Java—IO流 字符流

    java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码). 文件是byte byte byte ... 的数据序列. 文本文件是文本(char)序列按照某种编码方案(uf ...

  4. 关于“为什么不加friend就会提示参数过多”

    #include <iostream> using namespace std; class Complex { double real, imag; public: Complex(do ...

  5. LeetCode-Maximal Rectangle[code]

    code: #include <iostream> #include <vector> #include <stack> #include <algorith ...

  6. Docker_1 安装Docker-CE

    安装 免sudo运行docker命令 ustc mirrors service failed 安装 Docker-CE 安装过程参考官网,Ubuntu中如下: ## 1. 从仓库安装 $ sudo a ...

  7. 个人Hadoop编程代码记录

    **WordCount package cn.cpl.recom; import java.io.IOException; import java.util.StringTokenizer; impo ...

  8. YII2.0安装教程,数据库配置前后台 [ 2.0 版本 ]

    1.首先下载yii-advanced-app-2.0.6.tgz 2.解压到D:\wamp\www\yii2目录下面将目录advanced下所有文件剪切到 D:\wamp\www\yii2 3.打开c ...

  9. 学习Road map Part 03 编程和算法

    方法: 优先重复已学过的内容 写学习笔记

  10. Linux 系统查看tomcat控制台命令

    前提进入tomcat/logs文件夹下 查看全部命令是:tail -f catalina.out 如果想查看具体文件的日志进入该文件所在目录然后命令如下: tail -f filename