`

counter_cache的使用

阅读更多
        有点惭愧,做rails也2年了,竟然不会用counter_cache,刚才google了counter_cache,感觉这个还挺有用的,举个例子,在sina微博中,微博的评论数和收藏数就可以使用到counter_cache,使用了counter_cache,我们就不需要在数据库中查找该博客下的所有评论,然后再计算评论数了,举个具体的例子吧.
       class Blog < ActiveRecord::Base
           has_many :comments
       end

       class Comment < ActiveRecord::Base
           belongs_to :blog, :counter_cache => true
       end

       注意:  blogs中有一个字段是comments_count(整型),当添加和删除comment的时候,会在它所对应的blogs中的comments_count加或减1,这里需要注意的是destroy和destroy_all,可以触发blogs中的comments_count加或减1,而delete和delete_all则不会。

       此外如果要指定特定的名字的话呢,就 :counter_cache => :pinlun_counts,这样在blogs中就使用pinlun_counts,而不是comments_count.
  
      
分享到:
评论

相关推荐

    custom_counter_cache:支持条件和多种模型的自定义counter_cache功能

    gem 'custom_counter_cache' 示例 带计数器缓存的类 这是将用于计算计数器缓存的值的块。 其他模型将通过after_save或after_destroy回调通过它们的关联来调用它。 include CustomCounterCache :: Model define_...

    counter_culture:Rails应用程序的涡轮增压计数器缓存

    反文化 Turbo为您的Rails应用程序提供计数器缓存。 对Rails标准计数器缓存的巨大改进...product.categories_count # =&gt; will use counter cache without query 安装 将counter_culture添加到您的Gemfile中: gem 'co

    Laravel开发-lara-counter-cache

    Laravel开发-lara-counter-cache Laravel的计数器缓存

    Laravel开发-counter-cache

    Laravel开发-counter-cache Laravel的计数器缓存

    mongoid-magic-counter-cache:用于Mongoid ORM的简单计数器缓存宝石

    RDOC 安装Mongoid Magic Counter Cache至少需要ruby 1.9.3Ruby$ [sudo] gem install mongoid_magic_counter_cache宝石文件gem 'mongoid_magic_counter_cache'用法首先,将一个字段添加到文档中,您将从中访问计数器...

    Laravel开发-lara-counter-cache .zip.zip

    Laravel开发-lara-counter-cache .zip.zip

    Laravel开发-laravel-delayed-counter-cache

    Laravel开发-laravel-delayed-counter-cache 延迟的计数器缓存在事务中增加/减少

    Smarty中文手册 chm版

    $cache_modified_check [缓存修改检查变量] $config_overwrite [配置覆盖变量] $config_booleanize [配置布尔化变量] $config_read_hidden [配置读取隐藏变量] $config_fix_newlines [配置固定换行符变量] $...

    smarty手册.chm

    $cache_modified_check [缓存修改检查变量] $config_overwrite [配置覆盖变量] $config_booleanize [配置布尔化变量] $config_read_hidden [配置读取隐藏变量] $config_fix_newlines [配置固定换行符变量] $...

    post-views-counter:发布意见计数器

    发布意见计数器 帖子查看计数器可让您以简单,快速和可靠的方式显示查看过帖子,页面或自定义帖子类型的次数。 描述 可让您以简单,快速和可靠的方式显示过帖子,页面或自定义...兼容W3缓存/ WP SuperCache 可选的对象

    微软内部资料-SQL性能优化2

    System working set paging activity can be viewed by monitoring the Memory: Cache Faults/sec counter. For more information, see also… Inside Windows 2000,Third Edition, p. 463. System Cache The ...

    Distribute-Cache:基于redis的分布式缓存实现

    Cache&lt;String&gt; cache = CacheBuilder .newBuilder("redisCacheKeyPrefix") //redis中缓存值的key前缀 .expireTime(100) //过期时间 单位(秒) 默认过期时间为一年 .build(new CacheLoader() { @Override public...

    简易聊天室

    application.setAttribute("counter", Integer.toString(icount)); } %&gt; 您是第("counter") %&gt;位访问者! &lt;/body&gt; &lt;/html&gt; 简易聊天室本次实验的目的是通过以下题目掌握JSP内置对象,包括...

    缓存:缓存库

    快取 缓存是一个处理缓存后端的小型库,该库还处理计数器。使用Composer安装composer require sonata-project/cache缓存使用率&lt;...phpuse Sonata \ Cache \ Adapter \ Counter \ PRedisCounter

    汪文君高并发编程实战视频资源全集

     高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4  高并发编程第三阶段13讲 一个JNI程序的编写,通过Java去调用C,C++程序.mp4  高并发编程第三阶段14讲 Unsafe中的方法使用,一半是...

    汪文君高并发编程实战视频资源下载.txt

     高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4  高并发编程第三阶段13讲 一个JNI程序的编写,通过Java去调用C,C++程序.mp4  高并发编程第三阶段14讲 Unsafe中的方法使用,一半是...

    simple-tags

    (based on counter term) * Fix bug for allow Suggested Tags for page CPT. * Version 2.1 : * Add compatibility with WP 3.2 * Fix bug with autocompletion. (jQuery want now a strict content type text/...

    kgb档案压缩console版+源码

    KGB Archiver console version ?...based on PAQ6 by Matt Mahoney PAQ6v2 - File archiver and compressor. (C) 2004, Matt Mahoney, mmahoney@cs.fit.edu This program is free software; you can redistribute it ...

    微软内部资料-SQL性能优化3

    Contents Overview 1 Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and ...

    EurekaLog_7.5.0.0_Enterprise

    EurekaLog 7.5 (18-August-2016) 1)..Important: Installation layout was changed. All packages now have version suffix (e.g. EurekaLogCore240.bpl). No files are copied to \bin folder of IDE....

Global site tag (gtag.js) - Google Analytics