ruby-on-rails - 是否可以使 Ohm for Ruby 中的整个对象的内容过期?

标签 ruby-on-rails ruby redis ohm

一旦发生我的特定事件,我希望能够使 Ruby on Rails 应用程序中基于欧姆的对象的全部内容过期。目前是否可以结合 Redis + expire 来执行此操作?使用 Ohm 时,对象有多个与之关联的键,包括索引等。我想确保所有内容都得到正确清理 - 这就是为什么我想知道是否有官方支持的方法来执行此操作。

最佳答案

不幸的是,没有。人们曾多次尝试解决这个难题,但我见过的每一次尝试都在 Ohm 数据集中留下了痕迹。它们都不具有独特的属性,据我所知,它们都使欧姆数据处于不一致的状态。

一些例子:

例如,当 Ohm 模型保存时,会有多个字段添加到 Redis 哈希中,并且还会将成员添加到 Redis 集中。虽然您可以对整个哈希或集设置过期时间,但不能使 Redis 哈希的单个字段或集合中的单个成员过期。整个哈希或集合都会过期。

这里是主要问题:如果这些集合和哈希过期,您将丢失模型上的整个索引,或者唯一属性的完整记录。因此,使用任何 Ohm 过期混合时的一个常见问题是,即使主数据 key 过期,对 find 的调用仍将从索引中返回一条记录,但哈希值为零。而且,如果您在模型中指定了唯一属性,则即使数据本身已过期,您也永远无法在该模型上使用过期值再次调用 create 而不引发异常。

Redis 中没有过期回调,因此当特定键过期时无法触发删除哈希字段或设置成员。有几个人请求允许哈希字段或集合成员在 Redis 问题列表中拥有 TTL,但他们都(相当合理地)以答案 such as this 结束。 :

Hi, this will not be implemented by original design:

No nested types in keys. No complex features in single fields of aggregate data types. The reasoning is much more complex and is a lot biased by personal feelings, preference and sensibility so there is no objective way I can show you this is better ;)

Closing. Thanks for reporting.

例如,以下是 Ohm 源代码 ( ohm.rb, 651-699 ) 中的一些注释:

  # The base class for all your models. In order to better understand
  # it, here is a semi-realtime explanation of the details involved
  # when creating a User instance.
  #
  # Example:
  #
  #   class User < Ohm::Model
  #     attribute :name
  #     index :name
  #
  #     attribute :email
  #     unique :email
  #
  #     counter :points
  #
  #     set :posts, :Post
  #   end
  #
  #   u = User.create(:name => "John", :email => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bed8d1d1fedcdfcc90ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>")
  #   u.incr :points
  #   u.posts.add(Post.create)
  #
  # When you execute `User.create(...)`, you run the following Redis
  # commands:
  #
  #   # Generate an ID
  #   INCR User:id
  #
  #   # Add the newly generated ID, (let's assume the ID is 1).
  #   SADD User:all 1
  #
  #   # Store the unique index
  #   HSET User:uniques:email <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8ded7d7f8dad9ca96dbd7d5" rel="noreferrer noopener nofollow">[email protected]</a> 1
  #
  #   # Store the name index
  #   SADD User:indices:name:John 1
  #
  #   # Store the HASH
  #   HMSET User:1 name John email <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ef8f1f1defcffecb0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>
  #
  # Next we increment points:
  #
  #   HINCR User:1:counters points 1
  #
  # And then we add a Post to the `posts` set.
  # (For brevity, let's assume the Post created has an ID of 1).
  #
  #   SADD User:1:posts 1
  #

但是人们通常尝试使欧姆数据过期的方式是使用像这样简单得多的方法(不操作唯一值或模型范围索引):

  Ohm.redis.expire(object.key, @expire)
  Ohm.redis.expire("#{object.key}:_indices", @expire)

总而言之,在 Redis 中对数据过期进行细粒度控制的最佳方法是使用低级接口(interface)(如 redis-rb)设计自己的存储方法。 .

关于ruby-on-rails - 是否可以使 Ohm for Ruby 中的整个对象的内容过期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016719/

相关文章:

ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT]

ruby-on-rails - 无法安装 recommendify gem - hiredis.h 未找到

ruby - Ruby 的 CSV.open 是否缓冲到内存并一次写入?

node.js - Redis 集群 : will hgetall search all nodes in cluster for key?

ruby-on-rails - Rails 3 和 ActiveAdmin。如何添加虚拟模型?

ruby-on-rails - Rails 5 中的密码确认

ruby-on-rails - 无法访问生产中的日志文件

ruby - 如何在不使用 Ruby 中的 "string.reverse"方法的情况下检查单词是否为回文?

performance - Redis 上的 MAX() SQL 等价物

node.js - Redis 同时进行 INCR、SORT 和 TTL?