简体   繁体   English

带Redis的Rails购物车:存储项目数量

[英]Rails shopping cart with Redis: storing item quantity

I'd like to store a product's quantity along with its ID using Redis. 我想使用Redis存储产品的数量及其ID。 Here's what I have right now: 这是我现在所拥有的:

  def add
    $redis.sadd current_user_cart, params[:product_id]
    redirect_to carts_show_path(current_user_cart)
    render json: current_user.cart_count, status: 200
  end

How can I store more than one value in a single key? 如何在一个键中存储多个值? Do I need to use a hash? 我需要使用哈希吗?

Yes. 是。 Redis is a key/value store. Redis是一个键/值存储。 Meaning that it only has 2 columns... One is the key and the second one is the value, which I would in your case define as a JSON over a hash. 这意味着它只有2列...一是键,第二是值,在您的情况下,我将其定义为散列上的JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM