在Redis中存储字典的字典(StackExchange.Redis) [英] Storing Dictionary of Dictionary in Redis (StackExchange.Redis)

查看:110
本文介绍了在Redis中存储字典的字典(StackExchange.Redis)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Redis中存储Dictionary of Dictionary.例如我有产品、订单、客户等...业务实体,它们都有 ID 和其他相关属性.

我使用的是最新版本的 StackExchage.Redis C#

期望:1. 当我用 ID 10 保存产品时,它应该首先检查是否可用的产品类型实体然后检查存在的产品 ID 10,如果是,则返回整个产品.2. 其他实体也一样.订单实体也可以使用 ID 10.

键:TypeName 值:Dict(int, Type)

每当发生写入时,不想更新整个字典,只想添加新记录或更新字典中的记录.

所以,1. 每当请求所有产品时,我都可以返回内部字典.值2.如果要求单个产品,那么我可以退回一个产品3.如果要求删除所有产品,那么它将一次性删除所有内容.4. 使用HashSet为每个ProductIds设置超时时间?

解决方案

看来你需要 Redis哈希.您可以为产品、订单等创建哈希值,并通过它们的键存储每个项目.这是一个例子:

  • 将产品添加到哈希/字典中

 redis>HSET 产品 id:10 "{product_10_json}" id:11 "{product_11_json}"(整数)2

  • 通过 ID 检索单个产品.

 redis>HGET 产品编号:10{product_10_json}"

  • 获取所有产品列表

 redis>HGETALL 产品1)ID:10"2){product_10_json}"3)身份证:11"4){product_11_json}"

  • 删除项目产品列表:
<预><代码>Redis>Hdel 产品编号:10(整数)1Redis>HGETALL 产品1)ID:11"2){product_11_json}"

  • 要删除所有产品只需删除相应的键

 redis>德尔产品(整数)1

计算一个键中的项目数:

<块引用>

是否有任何命令可以让我计算总价值?比如产品有 15 条记录

你应该使用 HLEN

redis>HSET产品product1你好"(整数)1Redis>HSET产品product2世界"(整数)1Redis>海伦产品(整数)2

I want to store Dictionary of Dictionary in Redis. For e.g. I have Product, Order, Customer etc... business entity and they all have Id and other relevant properties.

I am using latest version of StackExchage.Redis C#

Expectation: 1. When I Save product with ID 10 then it should first check if Product type entity available then check Product Id 10 present, If yes then return whole product. 2. Same for other entities. Id 10 may be available for Order entity as well.

Key: TypeName Value: Dict(int, Type)

whenever any writes happens, don't want to update whole dictionary, just want to add new record or update record in a dictionary.

So, 1. whenever all product are requested, i can return inner dictionary.Values 2. If individual product requested then i can return one Product 3. If requested for all product deletes then it will delete everything in one go. 4. Set time out for each ProductIds using HashSet?

解决方案

It seems you need Redis Hashes. You can create hashes for products, order.. etc. and store each item by their key. here is an example:

  • adding product into hash/Dictionary

    redis>  HSET product id:10 "{product_10_json}" id:11 "{product_11_json}"
    (integer) 2

  • retrive an individual product by id.

    redis> HGET product id:10
    "{product_10_json}"

  • fetch all product list

    redis> HGETALL product
    1) "id:10"
    2) "{product_10_json}"
    3) "id:11"
    4) "{product_11_json}"

  • Delete an item product list:


    redis> Hdel product id:10
    (integer) 1
    redis> HGETALL product
    1) "id:11"
    2) "{product_11_json}"

  • TO delete all product just delete corresponding key

    redis> del product
    (integer) 1

Edit: Count the number of items in a key:

Is there any command which will give me total values count? like product has 15 records

You should use HLEN

redis>HSET product product1 "Hello"
(integer) 1
redis> HSET product product2 "World"
(integer) 1
redis> HLEN product
(integer) 2

这篇关于在Redis中存储字典的字典(StackExchange.Redis)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆