Tag: 反缓存

如何持续增加计数器缓存列?

假设我有一个计数器缓存,需要在每次页面加载时递增。 假设我有10个Web实例。 如何持续增加计数器缓存列? 一个Web实例可以轻松实现一致性。 但是在多个实例运行时,可能会出现竞争条件。 这是一个快速解释。 假设我的计数器缓存列称为foo_counts ,其起始值为0.如果同时加载2个Web实例,则两者都将计数视为0.当需要增加计数时。 它们都将计数从0增加到1。 我查看了http://guides.rubyonrails.org/active_record_querying.html#locking-records-for-update 任何想法将不胜感激。

计数器缓存不会更新,但我可以保存到父级和子级

我添加了一个计数器缓存,但无法让它更新。 但我可以通过添加新的博客post来更新父级 – 博客文章模型 – 我可以通过添加新评论来更新孩子 – 评论模型。 计数器缓存应该通过自动更新blog_posts.comments_count字段来跟踪每篇博文的评论总数。 我将概述我经历的一些步骤,希望有人会注意到我做错了什么。 架构转储结束。 我有一个Blog Post模型: class Post “User”, :foreign_key => ‘author_id’ has_many :comments, :class_name => “Comment”, :foreign_key => ‘post_id’, :order => “created_at desc”, :dependent => :destroy has_many :categorizations has_many :categories, :through => :categorizations named_scope :recent, :order => “created_at desc”, :limit => 5 end 以及将counter_cache设置为post模型的Comments模型: class Comment “Post”, […]

:counter_cache => true表示存储和

我有表users和scores 。 这是协会: belongs_to :user #score model has_many :scores #user model 表users具有名为scores_count的列。 在此列中,我将所有值的总和存储在表scores 。 我想用这种方式在列scores_count: :counter_cache => true存储所有scores的总和 但是:counter_cache => true只保存表scores的行scores 。 是否有类似的方法来存储表scores中所有值的总和? 或者我必须自己实施这项任务?

save(:validate => false)涵盖了什么?

我刚刚使用如下代码实现了一些自定义counter_cache : def after_save self.update_counter_cache end def after_destroy self.update_counter_cache end def update_counter_cache self.company.new_matchings_count = Matching.where(:read => false).count self.company.save end 我的问题是 – 命令Model.save(:validate => false)实际上阻​​止了validates_with或before_validation类的东西? 如果我保留现有的保存而不进行validation,我的自定义counter_caches会受到影响吗?

Rails 4:has_many中的counter_cache:通过与dependent :: destroy的关联

虽然已经提出了类似的问题: 带有has_many的counter_cache:通过 dependent =>在“has_many through”关联上销毁 has_many:通过counter_cache 他们都没有真正解决我的问题。 我有三个模型,有一个has_many:通过关联: class User < ActiveRecord::Base has_many :administrations has_many :calendars, through: :administrations end class Calendar < ActiveRecord::Base has_many :administrations has_many :users, through: :administrations end class Administration < ActiveRecord::Base belongs_to :user belongs_to :calendar end 联接管理模型具有以下属性: id user_id calendar_id role 我想计算每个user有多少个calendars以及每个calendar有多少users 。 我打算使用counter_cache,如下所示: class Administration < ActiveRecord::Base belongs_to :user, counter_cache: :count_of_calendars […]