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

时间:2021-08-14 12:32:28

I'd like to store a product's quantity along with its ID using Redis. Here's what I have right now:

我想使用Redis存储产品的数量及其ID。这就是我现在所拥有的:

  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?

如何在一个键中存储多个值?我需要使用哈希吗?

1 个解决方案

#1


1  

Yes. Redis is a key/value store. 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.

是。 Redis是一家钥匙/超值商店。这意味着它只有2列...一个是键,第二个是值,在您的情况下,我将在哈希定义为JSON。

#1


1  

Yes. Redis is a key/value store. 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.

是。 Redis是一家钥匙/超值商店。这意味着它只有2列...一个是键,第二个是值,在您的情况下,我将在哈希定义为JSON。