Storing Dictionary of Dictionary in Redis (StackExchange.Redis)

给你一囗甜甜゛ 提交于 2019-12-11 07:46:02

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/57308747/storing-dictionary-of-dictionary-in-redis-stackexchange-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!