redis基本

引用:ServiceStack.Redis.dll


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
namespace RedisDemo
{
  /// <summary>
  /// RedisManager类主要是创建链接池管理对象的
  /// </summary>
  public class RedisManager
  {
    /// <summary>
    /// redis配置文件信息
    /// </summary>
    private static string RedisPath ="127.0.0.1:6379";// System.Configuration.ConfigurationSettings.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 = 5, // “写”链接池链接数 
        MaxReadPoolSize = 5, // “读”链接池链接数 
        AutoStart = true,
      });
    }
   
    /// <summary>
    /// 客户端缓存操作对象
    /// </summary>
    public static IRedisClient GetClient()
    {
      if (_prcm == null)
      {
        CreateManager();
      }
      return _prcm.GetClient();
    }
  }
}


------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using ServiceStack.Redis.Support;
namespace RedisDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        //获取Redis操作接口
        IRedisClient Redis = RedisManager.GetClient();
        
        
        //放入内存
        Redis.Set<string>("my_name", "小张");
        Redis.Set<int>("my_age", 12);
        //保存到硬盘
        Redis.Save();
        //释放内存
        Redis.Dispose();
       
        
        //取出数据
        Console.WriteLine("取出刚才存进去的数据 \r\n 我的Name:{0}; 我的Age:{1}.",
          Redis.Get<string>("my_name"), Redis.Get<int>("my_age"));
        Console.ReadKey();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message.ToString());
        Console.ReadKey();
      }
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Redis数据库是一个内存数据库,通过key-value键值对的方式进行数据存储。它的优点包括访问速度快、适用于缓存系统和存储大量活跃数据,可以提高网站的响应速度。\[1\]在Java中,有两个比较出名的连接Redis的驱动包,分别是Jedis和Redisson,我们可以使用Jedis来进行程序操作Redis。\[3\]以下是一些基本Redis操作方法: - 使用`new Jedis(host, port)`创建Jedis对象,其中host是Redis服务器地址,port是Redis服务端口。 - 使用`set(key, value)`方法设置字符串类型的数据。 - 使用`get(key)`方法获取字符串类型的数据。 - 使用`hset(key, field, value)`方法设置哈希类型的数据。 - 使用`hget(key, field)`方法获取哈希类型的数据。 - 使用`lpush(key, values)`方法设置列表类型的数据。 - 使用`lpop(key)`方法从列表左侧弹出元素。 - 使用`rpop(key)`方法从列表右侧弹出元素。 - 使用`del(key)`方法删除指定的key。\[3\] #### 引用[.reference_title] - *1* [redis基本操作](https://blog.csdn.net/m0_50782114/article/details/128119748)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Redis基本使用!](https://blog.csdn.net/weixin_50769390/article/details/127134134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值