参考自:https://blog.csdn.net/only_yu_yy/article/details/78873735

https://blog.csdn.net/fenghuoliuxing990124/article/details/84983694

 using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text; namespace RedisDemo
{
class StringDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient(); //---字符串---
//set key value
//summary: Set the string value of a key
client.Set<int>("pwd", );
//get key
//summary: Get the value of a key
int pwd = client.Get<int>("pwd");
Console.WriteLine(pwd); //---对象---
var todos = client.As<Todo>();
Todo todo = new Todo
{
Id = todos.GetNextSequence(),
Content = "String Demo",
Order =
};
client.Set<Todo>("todo", todo);
var getTodo = client.Get<Todo>("todo");
Console.WriteLine(getTodo.Content);
}
}
}

String的应用场景

计数器:许多运用都会使用redis作为计数的基础工具,他可以实现快速计数、查询缓存的功能。

比如:优酷视频的播放:incr video:videoId:playTimes

或者:文章浏览量:incr article:aricleId:clickTimes

或者粉丝数量:取关 decr author:authorId:fansNumber

 using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text; namespace RedisDemo
{
class HashDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient(); //HSET key field value
//summary: Set the string value of a hash field
client.SetEntryInHash("test", "name", "ermao");
client.SetEntryInHash("test", "age", ""); //---获取test哈希下的所有key---
//HKEYS key
//summary: Get all the fields in a hash
List<string> hashKeys = client.GetHashKeys("test");
Console.WriteLine("keys in test");
foreach (var item in hashKeys)
{
Console.WriteLine(item);
} //---获取test哈希下的所有值---
//HVALS key
//summary: Get all the values in a hash
List<string> hashValues = client.GetHashValues("test");
Console.WriteLine("values in test");
foreach (var item in hashValues)
{
Console.WriteLine(item);
} //---获取test哈希下,第一个Key对应的值---
//HGET key field
//summary: Get the value of a hash field
string value = client.GetValueFromHash("test", hashKeys[]);
Console.WriteLine($"test下的key{hashKeys[0]}对应的值{value}");
}
}
}

Hash的应用场景

商品详情页

4、Redis基本数据类型:List
list是一个链表结构,key可以理解为链表的名字,然后往这个名字所对应的链表里加值。,list可以以队列 / 栈的形式进行工作。

 using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text; namespace RedisDemo
{
class ListDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient(); //---队列的使用(先进先出)---
client.EnqueueItemOnList("name", "zhangsan");
client.EnqueueItemOnList("name", "lisi");
long count = client.GetListCount("name");
for (int i = ; i < count; i++)
{
Console.WriteLine(client.DequeueItemFromList("name"));
} //---栈的使用(先进后出)---
client.PushItemToList("course", "Math");
client.PushItemToList("course", "English");
long count2 = client.GetListCount("course");
for (int i = ; i < count2; i++)
{
Console.WriteLine(client.PopItemFromList("course"));
}
}
}
}

List的应用场景

点赞:
创建一条微博内容:set user:1:post:91 “hello redis”;
点赞:
lpush post:91:good “kobe.png”
lpush post:91:good “jordan.png”
lpush post:91:good “James.png”
查看有多少人点赞:llen post:91:good
查看有哪些人点赞:lrange post:91:good 0 -1

 using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text; namespace RedisDemo
{
class SetDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient(); //SADD key member [member ...]
//summary: Add one or more members to a set
client.AddItemToSet("s1", "abc");
client.AddItemToSet("s1", "qwer");
client.AddItemToSet("s1", "asdf");
client.AddItemToSet("s1", "hjkl");
client.AddItemToSet("s1", "zxc");
//SMEMBERS key
//summary: Get all the members in a set
HashSet<string> hashSet = client.GetAllItemsFromSet("s1");
foreach (var item in hashSet)
{
Console.WriteLine(item);
} client.AddItemToSet("s2", "qwer");
client.AddItemToSet("s2", "wasd"); //SUNION key [key ...]
//summary: Add multiple sets
HashSet<string> hashSetUnion = client.GetUnionFromSets(new string[] { "s1", "s2" });
Console.WriteLine("---并集---");
foreach (var item in hashSetUnion)
{
Console.WriteLine(item);
} //SINTER key [key ...]
//summary: Intersect multiple sets
HashSet<string> hashSetInter = client.GetIntersectFromSets(new string[] { "s1", "s2" });
Console.WriteLine("---交集---");
foreach (var item in hashSetInter)
{
Console.WriteLine(item);
} //SDIFF key [key ...]
//summary: Subtract multiple sets
HashSet<string> hashSetDifference = client.GetDifferencesFromSet("s1", new string[] { "s2" });
Console.WriteLine("---差集---");
foreach (var item in hashSetDifference)
{
Console.WriteLine(item);
}
}
}
}

Set的应用场景

随机事件(如:抽奖)、共同好友、推荐好友等

6、Redis基本数据类型:SortedSet
set是一种非常方便的结构,但是数据无序,redis提供了一个sorted set,每一个添加的值都有一个对应的分数,放进去的值按照该分数升序存在一个集合中,可以通过这个分数进行相关排序的操作。

 using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text; namespace RedisDemo
{
class SortedSetDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient(); //ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
//summary: Add one or more members to a sorted set, or update its score if it already exists
client.AddItemToSortedSet("grade", "Chinese", );
client.AddItemToSortedSet("grade", "Math", );
client.AddItemToSortedSet("grade", "English", );
client.AddItemToSortedSet("grade", "History", );
//ZREVRANGE key start stop [WITHSCORES]
//summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
List<string> sortedList = client.GetAllItemsFromSortedSetDesc("grade");
foreach (var item in sortedList)
{
Console.WriteLine(item);
}
}
}
}

SortedSet的应用场景

排行榜(如:微博热搜排行榜)

参考自:https://blog.csdn.net/only_yu_yy/article/details/78873735
    https://blog.csdn.net/fenghuoliuxing990124/article/details/84983694

1、使用的Redis客户端为:ServiceStack.Redis
打开“程序包管理器控制台”,输入并执行“Install-Package ServiceStack.Redis”即可。

2、Redis基本数据类型:String
String类型是最常用的数据类型,在Redis中以KKey/Value存储。

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisDemo
{
    class StringDemo
    {
        public static void Start()
        {
            var redisMangement = new RedisManagerPool("127.0.0.1:6379");
            var client = redisMangement.GetClient();

//---字符串---
            //set key value
            //summary: Set the string value of a key
            client.Set<int>("pwd", 111);
            //get key
            //summary: Get the value of a key
            int pwd = client.Get<int>("pwd");
            Console.WriteLine(pwd);

//---对象---
            var todos = client.As<Todo>();
            Todo todo = new Todo
            {
                Id = todos.GetNextSequence(),
                Content = "String Demo",
                Order = 1
            };
            client.Set<Todo>("todo", todo);
            var getTodo = client.Get<Todo>("todo");
            Console.WriteLine(getTodo.Content);
        }
    }
}

1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

String的应用场景

计数器:许多运用都会使用redis作为计数的基础工具,他可以实现快速计数、查询缓存的功能。

比如:优酷视频的播放:incr video:videoId:playTimes

或者:文章浏览量:incr article:aricleId:clickTimes

或者粉丝数量:取关 decr author:authorId:fansNumber

3、Redis基本数据类型:Hash
Hash在Redis采用 (HashId,Key,Value)进行存储,一个HashId 可以包含多个key,一个key对应着一个value。

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisDemo
{
    class HashDemo
    {
        public static void Start()
        {
            var redisMangement = new RedisManagerPool("127.0.0.1:6379");
            var client = redisMangement.GetClient();

//HSET key field value
            //summary: Set the string value of a hash field
            client.SetEntryInHash("test", "name", "ermao");
            client.SetEntryInHash("test", "age", "26");

//---获取test哈希下的所有key---
            //HKEYS key
            //summary: Get all the fields in a hash
            List<string> hashKeys = client.GetHashKeys("test");
            Console.WriteLine("keys in test");
            foreach (var item in hashKeys)
            {
                Console.WriteLine(item);
            }

//---获取test哈希下的所有值---
            //HVALS key
            //summary: Get all the values in a hash
            List<string> hashValues = client.GetHashValues("test");
            Console.WriteLine("values in test");
            foreach (var item in hashValues)
            {
                Console.WriteLine(item);
            }

//---获取test哈希下,第一个Key对应的值---
            //HGET key field
            //summary: Get the value of a hash field
            string value = client.GetValueFromHash("test", hashKeys[0]);
            Console.WriteLine($"test下的key{hashKeys[0]}对应的值{value}");
        }
    }
}

1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47

Hash的应用场景

商品详情页

4、Redis基本数据类型:List
list是一个链表结构,key可以理解为链表的名字,然后往这个名字所对应的链表里加值。,list可以以队列 / 栈的形式进行工作。

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisDemo
{
    class ListDemo
    {
        public static void Start()
        {
            var redisMangement = new RedisManagerPool("127.0.0.1:6379");
            var client = redisMangement.GetClient();

//---队列的使用(先进先出)---
            client.EnqueueItemOnList("name", "zhangsan");
            client.EnqueueItemOnList("name", "lisi");
            long count = client.GetListCount("name");
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(client.DequeueItemFromList("name"));
            }

//---栈的使用(先进后出)---
            client.PushItemToList("course", "Math");
            client.PushItemToList("course", "English");
            long count2 = client.GetListCount("course");
            for (int i = 0; i < count2; i++)
            {
                Console.WriteLine(client.PopItemFromList("course"));
            }
        }
    }
}

1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34

List的应用场景

点赞:
创建一条微博内容:set user:1:post:91 “hello redis”;
点赞:
lpush post:91:good “kobe.png”
lpush post:91:good “jordan.png”
lpush post:91:good “James.png”
查看有多少人点赞:llen post:91:good
查看有哪些人点赞:lrange post:91:good 0 -1

5、Redis基本数据类型:Set
它是去重、无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集。

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisDemo
{
    class SetDemo
    {
        public static void Start()
        {
            var redisMangement = new RedisManagerPool("127.0.0.1:6379");
            var client = redisMangement.GetClient();

//SADD key member [member ...]
            //summary: Add one or more members to a set
            client.AddItemToSet("s1", "abc");
            client.AddItemToSet("s1", "qwer");
            client.AddItemToSet("s1", "asdf");
            client.AddItemToSet("s1", "hjkl");
            client.AddItemToSet("s1", "zxc");
            //SMEMBERS key
            //summary: Get all the members in a set
            HashSet<string> hashSet = client.GetAllItemsFromSet("s1");
            foreach (var item in hashSet)
            {
                Console.WriteLine(item);
            }

client.AddItemToSet("s2", "qwer");
            client.AddItemToSet("s2", "wasd");

//SUNION key [key ...]
            //summary: Add multiple sets
            HashSet<string> hashSetUnion = client.GetUnionFromSets(new string[] { "s1", "s2" });
            Console.WriteLine("---并集---");
            foreach (var item in hashSetUnion)
            {
                Console.WriteLine(item);
            }

//SINTER key [key ...]
            //summary: Intersect multiple sets
            HashSet<string> hashSetInter = client.GetIntersectFromSets(new string[] { "s1", "s2" });
            Console.WriteLine("---交集---");
            foreach (var item in hashSetInter)
            {
                Console.WriteLine(item);
            }

//SDIFF key [key ...]
            //summary: Subtract multiple sets
            HashSet<string> hashSetDifference = client.GetDifferencesFromSet("s1", new string[] { "s2" });
            Console.WriteLine("---差集---");
            foreach (var item in hashSetDifference)
            {
                Console.WriteLine(item);
            }
        }
    }
}

1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61

Set的应用场景

随机事件(如:抽奖)、共同好友、推荐好友等

6、Redis基本数据类型:SortedSet
set是一种非常方便的结构,但是数据无序,redis提供了一个sorted set,每一个添加的值都有一个对应的分数,放进去的值按照该分数升序存在一个集合中,可以通过这个分数进行相关排序的操作。

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisDemo
{
    class SortedSetDemo
    {
        public static void Start()
        {
            var redisMangement = new RedisManagerPool("127.0.0.1:6379");
            var client = redisMangement.GetClient();

//ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
            //summary: Add one or more members to a sorted set, or update its score if it already exists
            client.AddItemToSortedSet("grade", "Chinese", 82);
            client.AddItemToSortedSet("grade", "Math", 96);
            client.AddItemToSortedSet("grade", "English", 91);
            client.AddItemToSortedSet("grade", "History", 97);
            //ZREVRANGE key start stop [WITHSCORES]
            //summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
            List<string> sortedList = client.GetAllItemsFromSortedSetDesc("grade");
            foreach (var item in sortedList)
            {
                Console.WriteLine(item);
            }
        }
    }
}

1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

SortedSet的应用场景

排行榜(如:微博热搜排行榜)
————————————————
版权声明:本文为CSDN博主「weixin_41834782」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41834782/article/details/107555138

基于.Net Core的Redis:基本数据类型及其应用场景与命令行操作的更多相关文章

  1. Redis各种数据类型的应用场景

    redis是一种key values形式的非关系型数据库,通过内存存储,也可以把数据持久化到本地文件中. redis支持丰富的数据类型,String,list,set,zset,hash,下面说一下各 ...

  2. redis的数据类型与应用场景(二)

    1. 如何学习 redis有好多数据类型,有这么多数据类型,我们不可能每个都记得完完全全.但是我们必须知道它有哪些数据类型,每个数据类型是怎样的,有什么作用.redis的每一个数据类型都有一大堆命令, ...

  3. Redis各种数据类型的使用场景

    Redis的六种特性 l Strings l Hashs l Lists l Sets l Sorted Sets l Pub/Sub Redis各特性的应用场景 Strings Strings 数据 ...

  4. 一文搞定Redis五大数据类型及应用场景

    本文学习知识点 redis五大数据类型数据类型:string.hash.list.set.sorted_set 五大类型各自的应用场景 @TOC 1. string类型 1-1 string类型数据的 ...

  5. Redis的数据类型及使用场景

    1.redis 的数据类型 String 字符串 Hash 哈希 List 列表 Set 集合 ZSet(Sorted Set) 有序集合 2.使用场景 2.1 String 用户token 可以用r ...

  6. Redis的数据类型以及应用场景

    1. Redis的作用 1.1 Redis可以做什么 1.缓存:缓存机制几乎在所有的大型网站都有使用,合理地使用缓存不仅可以加快数据的访问速度,而且能够有效地降低后端数据源的压力.Redis提供了键值 ...

  7. Redis进阶实践之十五 Redis-cli命令行工具使用详解第二部分(结束)

    一.介绍           今天继续redis-cli使用的介绍,上一篇文章写了一部分,写到第9个小节,今天就来完成第二部分.话不多说,开始我们今天的讲解.如果要想看第一篇文章,地址如下:http: ...

  8. Redis进阶实践之十四 Redis-cli命令行工具使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...

  9. Redis进阶实践之十四 Redis-cli命令行工具使用详解第一部分

    一.介绍       redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redis的东西要看的都差不多看完了.网上的东西也不多了.剩下来就看看官网的东西吧,一遍翻译,一遍测试. ...

随机推荐

  1. php - 二维数组转一维数组总结

    二维数组转一维数组总结 例如将如下二位数组转以为以为一维数组 $records = [ [ 'id' => 2135, 'first_name' => 'John', 'last_name ...

  2. 一场由yield引发的连串拷问

    最近在学习Python中生成器时,遇到了一个yield关键词,廖雪峰老师的官网中也没有详细的解释,经过一番查阅和研究,终于对它有了一些认识并做了总结(如有不对之处,还请大神指正). 首先先简单了解下生 ...

  3. Spring Boot是什么?

    背景 最近因公司需要,开始研究java相关的开发,之前一直从事.net相关开发,所以写的或者理解的不对的地方呢,希望大家批评指正. 首先开发框架吧,就像.net很早之前有asp.net webForm ...

  4. 如何去除List集合中重复的元素

    1.通过循环进行删除 public static void removeDuplicate(List list) { for ( int i = 0 ; i < list.size() - 1 ...

  5. 2020年的六种编程语言排名中,java排第几只有不到1%的人知道

    前言 编程语言是开发的基础.有不同的类型和特征,并且开发人员针对不同的场景选择正确的语言,但是您知道使用哪种语言吗?中国和世界各地有多少开发人员正在使用它?他们的排名是多少?快来看看您知道多少个列表! ...

  6. java中执行cmd命令

    一.java执行cmd命令的三种方式:http://www.jb51.net/article/80829.htm 参考:https://www.cnblogs.com/zhufu9426/p/7928 ...

  7. java语言基础(八)_接口_多态

    接口 1. 接口定义的基本格式 接口就是多个类的公共规范,是一种引用数据类型,最重要的内容就是其中的:抽象方法. 如何定义一个接口的格式: public interface 接口名称 { // 接口内 ...

  8. 《Head First 设计模式》:观察者模式

    正文 一.定义 观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新. 要点: 观察者模式定义了对象之间一对多的关系. 观察者模式让主题(可观察者 ...

  9. redis基础02-redis的5种对象数据类型

    表格引用地址:http://www.cnblogs.com/xrq730/p/8944539.html 参考书籍:<Redis设计与实现>,<Redis运维与开发> 1.对象 ...

  10. Golden Tiger Claw(二分图)

    Golden Tiger Claw 题意 找到和最小的两个序列a,b满足对于任意i,j有a[i]+b[j]>=c[i][j](矩阵c给出). solution 裸的二分图就水过了-- #incl ...