zoukankan      html  css  js  c++  java
  • C# Redis写入程序

    直接贴代码,需要引用ServiceStack.Common.dll,ServiceStack.Interfaces.dll,ServiceStack.Redis.dll,ServiceStack.Text.dll

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ServiceStack.Redis;

    namespace RedisDemo
    {
    /// <summary>
    /// RedisManager类主要是创建链接池管理对象的
    /// </summary>
    public class RedisManager
    {
    /// <summary>
    /// redis配置文件信息
    /// </summary>
    public static string RedisPath = ConfigurationManager.AppSettings["RedisPath"];

    private static PooledRedisClientManager _prcm;

    /// <summary>
    /// 静态构造方法,初始化链接池管理对象
    /// </summary>
    static RedisManager()
    {
    CreateManager();
    }

    /// <summary>
    /// 创建链接池管理对象
    /// </summary>
    public static void CreateManager()
    {
    _prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
    }

    /// <summary>
    /// 关闭redis连接
    /// </summary>
    public void CloseCon()
    {
    try
    {
    _prcm.Dispose();
    Console.WriteLine("Redis Dispose...");
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    }
    }

    /// <summary>
    /// 设置redis
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool Set(string key, string value)
    {
    using (IRedisClient client = _prcm.GetClient())
    {
    return client.Set(key, value);
    }
    }

    private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
    {
    #region
    //WriteServerList:可写的Redis链接地址。
    //ReadServerList:可读的Redis链接地址。
    //MaxWritePoolSize:最大写链接数。
    //MaxReadPoolSize:最大读链接数。
    //AutoStart:自动重启。
    //LocalCacheTime:本地缓存到期时间,单位:秒。
    //RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。
    //RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应
    #endregion
    // 支持读写分离,均衡负载
    return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
    {
    MaxWritePoolSize = 50, // “写”链接池链接数
    MaxReadPoolSize = 50, // “读”链接池链接数
    AutoStart = true,
    });
    }

    public string Get(string value)
    {
    using (IRedisClient client = _prcm.GetClient())
    {
    return client.Get<string>(value);
    }
    }

    }
    }

  • 相关阅读:
    超酷图片压缩工具,就是不支持批量
    eclipse java热加载
    mysql 突然报错,连接不上
    svn问题终极解决办法
    svn经常困扰我的问题
    洛谷 P3628
    CodeForces 1091H
    委托的实际应用
    WPF 小知识点001
    C# 扩展方法一
  • 原文地址:https://www.cnblogs.com/wangjunguang/p/9494722.html
Copyright © 2011-2022 走看看