Redis学习笔记

本文详细介绍了如何在C#中使用ServiceStack.Redis库来连接和操作Redis,包括设置、获取、列表、哈希等基本操作,以及事务处理、并发锁的实现。此外,还提供了Redis在Windows上的下载和安装指南,以及Redis可视化工具的下载链接。
摘要由CSDN通过智能技术生成

Redis简介

redis服务端:
https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi

redis可视化工具:
https://github.com/uglide/RedisDesktopManager/releases/download/0.8.7/redis-desktop-manager-0.8.7.317.exe

Redis官方网址 http://redis.io
最新版Redis(windows版)下载网址 https://github.com/MSOpenTech/redis/releases
最新版redis可视化工具 下载网址 https://redisdesktop.com/download

下载安装Redis-x64-3.2.100.msi后,即可将redis-server.exe作为windows服务启动
Redis默认端口是 6379 ,正式环境请更改端口,以免被攻击。

安装过程
https://www.runoob.com/redis/redis-install.html

其它参考:
C#操作redis
Redis的使用初探
C# StackExchange.Redis 用法总结

1、连接Redis

在这里插入图片描述

使用NuGet安装ServiceStack.Redis,这是微软提供已经封装好的对redis操作类。在这里插入图片描述

因为ServiceStack从4.0+版本就开始收费了(免费版有每小时6000次请求限制),所以使用了PM > Install-Package ServiceStack -Version 3.9.71

关于ServiceStack介绍

提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛。首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Building Cross-Platform Web Services with ServiceStack,所以这里简要介绍一下如何使用ServiceStack快速建立一个WebService服务。

当然,在开始之前,首先要说明一下ServiceStack是个什么东西。 在国内用ServiceStack的似乎很少,大部分都是WCF或者ASP.NET WebAPI,唯一接触ServiceStack的可能是在C# 中调用Redis的时候,有个ServiceStack.Redis,之前还写过一篇 .NET中使用Redis 的拙文。这个ServiceStack.Redis其实就是ServiceStack的一个组件,专门用来跟Redis进行交互的,这部分内容引述自麦田守望者LEO

using ServiceStack.Redis;
using ServiceStack.Redis.Generic;

public static IRedisClientsManager clientsManager=null;
public static IRedisClient redisClient=null;

try
{
    #region 集群访问方式

    var host = String.Format("{2}@{0}:{1}", this.txtHost.Text, this.txtPort.Text, this.txtPwd.Text);
    host = String.Format("{0}:{1}", this.txtHost.Text, this.txtPort.Text);//v3.0格式
    clientsManager = new PooledRedisClientManager(new string[] {host});
    redisClient = clientsManager.GetClient();

    #endregion

    #region 单个实例方式

    //redisClient = new RedisClient(this.txtHost.Text, Int32.Parse(this.txtPort.Text),this.txtPwd.Text);

    #endregion
}
catch(Exception ex)
{
    MessageBox.Show("失败:"+ex.Message);
    return;
}
MessageBox.Show("成功");

2、对Redis操作

保存

 redisClient.Set(this.txtStringKey.Text, this.txtStringValue.Text);

读取

 var strValue=redisClient.Get<string>(this.txtStringKey.Text);
MessageBox.Show(strValue);

测试Add、Set、Replace

using (IRedisClient RClient = clientsManager.GetClient())
{
    RClient.Add("c1", "缓存1");
    RClient.Set("c1", "缓存2");
    RClient.Replace("c1", "缓存3");
    MessageBox.Show(RClient.Get<string>("c1"));
    RClient.Remove("c1");
    MessageBox.Show("移除状态:" + (RClient.Get<string>("c1") == null));
} 

测试List

using (IRedisClient RClient = clientsManager.GetClient())
{
    //内部维护一个List<T>集合
    RClient.AddItemToList("蜀国", "刘备");
    RClient.AddItemToList("蜀国", "关羽");
    RClient.AddItemToList("蜀国", "张飞");
    List<string> ListString = RClient.GetAllItemsFromList("蜀国");
    foreach (string str in ListString)
    {
        MessageBox.Show(str); //输出 刘备 关羽 张飞
    }
}

测试Hash

using (IRedisClient RClient = clientsManager.GetClient())
{
    //Hash<T>
    RClient.AddItemToSet("魏国", "曹操");
    RClient.AddItemToSet("魏国", "曹操");
    RClient.AddItemToSet("魏国", "典韦");
    HashSet<string> HashSetString = RClient.GetAllItemsFromSet("魏国");
    foreach (string str in HashSetString)
    {
        MessageBox.Show(str); //输出 典韦 曹操
    }
}

3、Redis事务

Redis Transaction测试


using (IRedisClient RClient = clientsManager.GetClient())
{
    RClient.Add("key", 1);
    using (IRedisTransaction IRT = RClient.CreateTransaction())
    {
        IRT.QueueCommand(r => r.Set("key", 20));
        IRT.QueueCommand(r => r.Increment("key", 1));

        IRT.Commit(); // 提交事务
    }
    MessageBox.Show(RClient.Get<string>("key"));
}

4、Redis并发锁

using (IRedisClient RClient = clientsManager.GetClient())
{
    RClient.Add("mykey", 1);
    // 支持IRedisTypedClient和IRedisClient
    using (RClient.AcquireLock("testlock"))
    {
        //Response.Write("申请并发锁<br/>");
        var counter = RClient.Get<int>("mykey");

        Thread.Sleep(100);

        RClient.Set("mykey", counter + 1);
        MessageBox.Show(RClient.Get<int>("mykey").ToString());
    }
}

5、Redis队列

队列写入

 redisClient.EnqueueItemOnList("operLog", "操作时间:" + DateTime.Now.ToString());
MessageBox.Show("写入成功");

队列读取

string msg = redisClient.DequeueItemFromList("operLog");
MessageBox.Show(msg);

更多关于redis操作方法请关注Redis官方网址 http://redis.io

尚硅谷是一个教育机构,他们提供了一份关于Redis学习笔记。根据提供的引用内容,我们可以了解到他们提到了一些关于Redis配置和使用的内容。 首先,在引用中提到了通过执行命令"vi /redis-6.2.6/redis.conf"来编辑Redis配置文件。这个命令可以让你进入只读模式来查询"daemonize"配置项的位置。 在引用中提到了Redis会根据键值计算出应该送往的插槽,并且如果不是该客户端对应服务器的插槽,Redis会报错并告知应该前往的Redis实例的地址和端口。 在引用中提到了通过修改Redis的配置文件来指定Redis的日志文件位置。可以使用命令"sudo vim /etc/redis.conf"来编辑Redis的配置文件,并且在文件中指定日志文件的位置。 通过这些引用内容,我们可以得出结论,尚硅谷的Redis学习笔记涵盖了关于Redis的配置和使用的内容,并提供了一些相关的命令和操作示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Redis学习笔记--尚硅谷](https://blog.csdn.net/HHCS231/article/details/123637379)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Redis学习笔记——尚硅谷](https://blog.csdn.net/qq_48092631/article/details/129662119)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值