Rails 使用 Redis 的购物车:存储商品数量
Rails shopping cart with Redis: storing item quantity
我想使用 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
如何在一个键中存储多个值?我需要使用哈希吗?
是的。
Redis 是一个 key/value 存储。这意味着它只有 2 列......
一个是键,第二个是值,在您的情况下,我将其定义为散列上的 JSON。
我想使用 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
如何在一个键中存储多个值?我需要使用哈希吗?
是的。 Redis 是一个 key/value 存储。这意味着它只有 2 列...... 一个是键,第二个是值,在您的情况下,我将其定义为散列上的 JSON。