如何使用klepto在磁盘上使用LRU缓存?(How to use LRU caching on disk with klepto?)
我正在尝试使用klepto来执行LRU缓存。 我想将缓存存储到磁盘,目前我正在使用klepto的
dir_archive
选项。 我写了下面的代码,主要基于klepto测试脚本中的代码:def mymap(data): return hashlib.sha256(data).hexdigest() class MyLRUCache: @lru_cache(cache=dir_archive(cached=False), keymap=mymap, ignore='self', maxsize=5) def __call__(self, data) return data call = __call__ def store(self, data): self.call(data) # I would also appreciate a better way to do this, if possible. def lookup(self, key): return self.call.__cache__()[key]
此代码似乎正常工作,直到缓存达到
maxsize
。 在这一点上,lru_cache
不是使用LRU来删除单个项目,lru_cache
清除整个缓存! 下面是一段klepto源代码( https://github.com/uqfoundation/klepto/blob/master/klepto/safe.py ):# purge cache if _len(cache) > maxsize: if cache.archived(): cache.dump() cache.clear() queue.clear() refcount.clear() else: # purge least recently used cache entry key = queue_popleft() refcount[key] -= 1 while refcount[key]: key = queue_popleft() refcount[key] -= 1 del cache[key], refcount[key]
所以我的问题是,为什么klepto清除“存档”缓存? 是否可以一起使用
lru_cache
和dir_archive
?另外,如果我的代码看起来完全是疯狂的,那么我真的很感激我应该写这个的一些示例代码,因为没有太多关于klepto的文档。
其他注意事项:我也试着用
cached=True
定义dir_archive
。 内存缓存在达到maxsize
大小时仍会被清除,但缓存的内容将转储到此时的归档缓存中。 我有几个问题:
- 内存中的高速缓存只有在达到
maxsize
大小时才是准确的,在此时它会被擦除。- 归档缓存不受
maxsize
影响。 内存缓存每次达到maxsize
大小时,内存缓存中的所有项目都将转储到存档缓存,而不管已存在多少个缓存。- 基于第1点和第2点,LRU缓存似乎是不可能的。
I am trying to use klepto to do LRU caching. I would like to store the cache to disk, and am currently using klepto's
dir_archive
option for this. I have written the following code, largely based on the code in the klepto test scripts:def mymap(data): return hashlib.sha256(data).hexdigest() class MyLRUCache: @lru_cache(cache=dir_archive(cached=False), keymap=mymap, ignore='self', maxsize=5) def __call__(self, data) return data call = __call__ def store(self, data): self.call(data) # I would also appreciate a better way to do this, if possible. def lookup(self, key): return self.call.__cache__()[key]
This code appears to work fine until the cache reaches
maxsize
. At that point, instead of using LRU to remove a single item,lru_cache
purges the entire cache! Below is the piece of klepto source code that does this (https://github.com/uqfoundation/klepto/blob/master/klepto/safe.py):# purge cache if _len(cache) > maxsize: if cache.archived(): cache.dump() cache.clear() queue.clear() refcount.clear() else: # purge least recently used cache entry key = queue_popleft() refcount[key] -= 1 while refcount[key]: key = queue_popleft() refcount[key] -= 1 del cache[key], refcount[key]
So my question is, why does klepto purge "archived" caches? Is it possible to use
lru_cache
anddir_archive
together?Also, if my code looks completely nuts, I would really appreciate some sample code of how I should be writing this, since there was not much documentation for klepto.
ADDITIONAL NOTES: I also tried defining
dir_archive
withcached=True
. The in-memory cache still gets purged whenmaxsize
is reached, but the contents of the cache are dumped to the archived cache at that point. I have several problems with this:
- The in-memory cache is only accurate until
maxsize
is reached, at which point it is wiped.- The archived cache is not affected by
maxsize
. Every timemaxsize
is reached by the in-memory cache, all items in the in-memory cache are dumped to the archived cache, regardless of how many are already there.- LRU caching seems impossible based on points 1 and 2.
原文:https://stackoverflow.com/questions/32081207
最满意答案
你可以将它们嵌套
map.resources :libraries do |library| library.resources :books do |book| book.resources :comments end end
这可能令人困惑,但数据模型的多态性方面与路由无关。
要通过命名路线调用它们,对于评论,您需要提供一个库和一本书,即使它是明确的。
You just nest them:
map.resources :libraries do |library| library.resources :books do |book| book.resources :comments end end
It may be confusing, but the polymorphic aspect of the data model is independent of the routes.
To call them via named routes, for a comment you'll need to provide a library and a book, even if it's unambiguous.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
类似的问题在这里: Django中的双重外键? 答案是在django中使用ContentTypes框架来实现多态关联。 similar question here: Double Foreign Key in Django? Answer is to use the ContentTypes framework in django to achieve polymorphic assocations.
-
Rails多态关联在同一模型上具有多个关联(Rails Polymorphic Association with multiple associations on the same model)[2021-10-19]
我在我的项目中做到了这一点。 诀窍在于,照片需要一列,用于在has_one条件下区分主要和次要照片。 注意发生的情况:conditions在这里。 has_one :photo, :as => 'attachable', :conditions => {:photo_type => 'primary_photo'}, :dependent => :destroy has_one :secondary_photo, :class_name => 'Photo', :as => 'attac ... -
Rails多态关联与同一模型错误上的多个关联(Rails Polymorphic Association with multiple associations on the same model bug)[2024-04-17]
image_type在这个多态关联中有一个特定的功能......它标识关联的模型(就像image_id标识该id)。 你不应该改变image_type因为它会破坏关联。 在图片模型中创建一个新的布尔列,说cover_picture ,你可以做... has_one :cover_picture, -> {where cover_picture: true} ... 这样做的好处是你的封面图片也包含在你的图片关联中,但是如果你想从has_many中排除该图片,那么你可以应用一个where子句以及... ha ... -
在轨道中保存多态模型(Saving Polymorphic model in rails)[2022-03-28]
# Either one of these Result.create(result_table: self) Result.create(result_table_id: self.id, result_table_type: self.class.to_s) # Either one of these Result.create(result_table: self) Result.create(result_table_id: self.id, result_table_type: self.cla ... -
你可以将它们嵌套 map.resources :libraries do |library| library.resources :books do |book| book.resources :comments end end 这可能令人困惑,但数据模型的多态性方面与路由无关。 要通过命名路线调用它们,对于评论,您需要提供一个库和一本书,即使它是明确的。 You just nest them: map.resources :libraries do |library| ...
-
我担心你的协会看起来完全错了。 首先,如果你在一个关联的一边有'has_many', 你必须在另一边有 'belongs_to'。 其次,我猜测done_user,admin_user等继承自User。 我对么 ? 参与者,admin_user等彼此之间有何不同? 你真的需要每个类的类,你能用命名范围吗? 我建议你简化你的数据模型。 现在你的建模模糊了。 请详细说明。 编辑 老实说,我认为你的建模过于复杂。 如果我是你,给定你对* _users的描述,我只会命名范围来检索该类型的用户。 所以实际上 clas ...
-
您的问题的答案: 如果您的业务登录意味着多个模型将具有相同字段的相关模型,那么您应该使用多态关联。 (在你的情况下,你可以使用它)。 如果设置多态关联,Rails将根据关联的父模型自动处理设置*_id和*_type字段。 假设您在多态关联中有许多订单的产品,并且您想要定义哪个模型订单属于: order = Order.first order.orderable Answers to your questions: If your business login implies that multiple m ...
-
1)如果将变量传递给partial,它将作为本地,而不是实例: <%= render :partial => 'comments/display', locals: {commentable: @article} %> 2)另请注意,从控制器渲染和从视图渲染是不同的事情。 在控制器中,您不必指定:partial=>因为Rails中的控制器应该呈现整个模板,但是如果您在视图文件中,则应该像Rails一样只能为每个请求呈现一个模板 1) If you pass variable to partial, it ...
-
Rails多形儿童(Rails Polymorphic Children)[2021-08-16]
您可能想要使用某种中间记录,比如ActivityItem ,它位于ActivityFeed和Photo , Comment等之间...... class ActivityFeed < ActiveRecord::Base has_many :activity_items end class ActivityItem < ActiveRecord::Base belongs_to :activity_feed belongs_to :item, :polymorphic => true end ...