0

假设我在Category班级中创建了一个新对象:

> @category = Category.create :name => "foo"
+----+------+-------------------------+-------------------------+
| id | name | created_at              | updated_at              |
+----+------+-------------------------+-------------------------+
| 13 | foo  | 2013-08-06 22:38:51 UTC | 2013-08-06 22:38:51 UTC |
+----+------+-------------------------+-------------------------+
1 row in set

如果它尚未存储在那里,我可以存储@category.namein的值:Rails.cache

> Rails.cache.fetch([@category, "name"]) {@category.name}
Cache read: categories/13-20130806223851/name
Cache generate: categories/13-20130806223851/name
Cache write: categories/13-20130806223851/name
=> "foo"

该记录具有唯一的缓存键categories/13-20130806223851/name.

我可以使用密钥读回该值。

> Rails.cache.read([@category, "name"])
Cache read: categories/13-20130806223851/name
=> "foo"

如果 I @category.touch,它的cache_key变化和查找不再检索存储的值。

> @category.touch
=> true

> Rails.cache.read([@category, "name"])
Cache read: categories/13-20130806224755/name
=> nil

这正是我们想要的。但是旧的键值对还在缓存中:

> Rails.cache.read("categories/13-20130806223851/name")
Cache read: categories/13-2013080

没有刷新我的整个缓存,如何确保自动删除旧的键值对?理想情况下,我正在寻找一种可以立即应用于我存储在缓存中的所有内容的解决方案。

顺便说一句,我在 Rails 3.2 和 Ruby 1.9.3 上。

4

0 回答 0