using System;
using System.Configuration;
using StackExchange.Redis; namespace Redis
{
public sealed class RedisDbContext
{
/// <summary>
/// Redis连接器
/// </summary>
private static Lazy<ConnectionMultiplexer> _lazyConnection; /// <summary>
/// Redis单例.
/// </summary>
/// <returns></returns>
public static IDatabase Instance()
{
return Instance(null);
} /// <summary>
/// Redis单例.
/// </summary>
/// <param name="connectionString">Redis服务器连接地址.</param>
/// <returns></returns>
public static IDatabase Instance(string connectionString)
{
return Instance(connectionString, );
} /// <summary>
/// Redis单例.
/// </summary>
/// <param name="connectionString">Redis服务器连接地址.</param>
/// <param name="db">Redis服务器数据库索引(0-10).</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">Redis服务器单例过程失败</exception>
public static IDatabase Instance(string connectionString, int db)
{
if (_lazyConnection == null)
{
lock (typeof(RedisDbContext))
{
if (_lazyConnection == null)
{
if (string.IsNullOrWhiteSpace(connectionString))
{
InitRedis();
}
else
{
InitRedis(connectionString);
}
}
}
} if (_lazyConnection == null)
{
throw new Exception("Redis服务器连接失败");
} return _lazyConnection.Value.GetDatabase(db);
} /// <summary>
/// 初始化Redis连接器.
/// </summary>
private static void InitRedis()
{
InitRedis(ConfigurationManager.AppSettings["RedisConnection"]);
} /// <summary>
/// 初始化Redis连接器.
/// </summary>
/// <param name="connectionString">Redis服务器连接地址.</param>
/// <exception cref="System.ArgumentNullException">Redis服务器连接地址ConnectionString未配置</exception>
private static void InitRedis(string connectionString)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException("Redis服务器地址未配置");
} _lazyConnection = new Lazy<ConnectionMultiplexer>(
() => ConnectionMultiplexer.Connect(new ConfigurationOptions()
{
AbortOnConnectFail = false,
EndPoints = { connectionString }
}));
}
}
}

Redis连接的更多相关文章

  1. Redis 连接问题

    .NET 中使用 StackExchange.Redis 我为什么想写这个,总感觉很多介绍相应技术的博客,只是把内容从官网搬到自己的博客中,没有任何的实践,这样会给想学的人,没有任何好处,也可能我是自 ...

  2. Redis 连接池的问题

      目录 Redis 连接池的问题    1 1.    前言    1 2.解决方法    1     前言 问题描述:Redis跑了一段时间之后,出现了以下异常. Redis Timeout ex ...

  3. PHP- 深入PHP、Redis连接

    pconnect, phpredis中用于client连接server的api. The connection will not be closed on close or end of reques ...

  4. Redis 连接

      Redis 连接命令主要是用于连接 redis 服务. 实例 以下实例演示了客户端如何通过密码验证连接到 redis 服务,并检测服务是否在运行: redis 127.0.0.1:6379> ...

  5. Python连接Redis连接配置

    1. 测试连接: Python 2.7.8 (default, Oct 20 2014, 15:05:19) [GCC 4.9.1] on linux2 Type "help", ...

  6. 2016022613 - redis连接命令集合

    redis连接命令 1.ping 用途:检查服务器是否正在运行 返回数据pong,表示服务器在运行. 2.quit 用途:关掉当前服务器连接 3.auth password 用途:服务器验证密码 没有 ...

  7. 红眼技术博客 » redis连接池红眼技术博客 » redis连接池

    红眼技术博客 » redis连接池 redis连接池

  8. redis连接池操作

    /** * @类描述 redis 工具 * @功能名 POJO * @author zxf * @date 2014年11月25日 */public final class RedisUtil { p ...

  9. java操作redis redis连接池

    redis作为缓存型数据库,越来越受到大家的欢迎,这里简单介绍一下java如何操作redis. 1.java连接redis java通过需要jedis的jar包获取Jedis连接. jedis-2.8 ...

  10. 三:Redis连接池、JedisPool详解、Redisi分布式

    单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...

随机推荐

  1. iOS空心圆下载进度指示器控件

    self.layer = [CAShapeLayer layer]; self.layer.frame = CGRectMake(, , , ); self.layer.position = self ...

  2. Atitit.css 规范 bem  项目中 CSS 的组织和管理

    Atitit.css 规范 bem  项目中 CSS 的组织和管理 1. 什么是BEM?1 1.1. 块(Block)2 1.2. 元素(Element)2 1.3. BEM树(和DOM树类似).3 ...

  3. properties属性文件工具类

    package xxx.business.utils; import org.apache.commons.configuration.ConfigurationException; import o ...

  4. VS 插件ReSharper10 破解注册方法(转)

    ReSharper 10.0.0.1 Ultimate 完美破解补丁使用方法,本资源来自互联网,感谢吾乐吧软件站的分享. ReSharper是一款由jetbrains开发的针对C#, VB.NET, ...

  5. Apache服务停止:信号灯超时时间已到,指定的网络名不再可用

    环境说明:Apache2.4.10,Windows Server 2008 R2 问题说明: apache服务用于下载文件,但是在运行一段时间后,突然挂了. 其错误提示如下所示: [error] (7 ...

  6. PHP基础语法

             PHP:超级文本预处理器一,PHP的四对标记 <?php ?> <script language="php"> <script> ...

  7. [LeetCode] Trips and Users 旅行和用户

    The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are b ...

  8. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构

      基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通用配置及服务调用, 提供了OA.CRM.CMS的原型实例,适合快速构建中小型互联网及行业 ...

  10. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...