Coder Social home page Coder Social logo

second_level_cache's Introduction

简介

SecondLevelCache is a write-through and read-through caching library inspired by Cache Money and cache_fu, support only Rails3 and ActiveRecord.

Read-Through: Queries by ID, like current_user.articles.find(params[:id]), will first look in cache store and then look in the database for the results of that query. If there is a cache miss, it will populate the cache.

Write-Through: As objects are created, updated, and deleted, all of the caches are automatically kept up-to-date and coherent.

使用

初始化redis:

# config/initializers/redis.rb
$redis = Redis.new

Gemfile

gem "hiredis", "~> 0.3.1"
gem "redis", "~> 2.2.0", :require =>redis ["redis/connection/hiredis", "redis"]
gem "second_level_cache"gem, :git => "[email protected]:hooopo/second_level_cache.git"

在model里设置是否使用缓存:

class User < ActiveRecord::Base
  acts_as_cached(:ttl => 3.day)
end

支持查询

  • User.find(1)
  • User.find_by_id(1), User.find_by_id!(1), User.find_by_id_and_name(1, "Hooopo"), User.where(:status => 1).find_by_id(1), user.articles.find_by_id(1)
  • user.articles.find(1), user.where(:status => 1).find(1), user.where("status = 1").find(1)
  • article.user

原理

每个查询之前,先判断该查询是否满足可缓存的条件:

  • 是否为主键查询,比如:User.find(1)
  • 没有join、lock、group、动态函数(sum/count/max等)
  • 是否为简单的条件查询(不含有in、between、>、<、!=、is等)。比如: User.where(:status => 1).where("level = 2").find(1)

如果满足以上条件,则先从缓存中读取记录,如果缓存中不存在记录,再到数据库中获取记录,并将得到的记录更新到缓存里。

在 after_commit, :on => :create/:update/:destroy 对记录的创建、更新、删除做监控。保证缓存内记录与数据库一致。

second_level_cache's People

Contributors

hooopo avatar

Watchers

James Cloos avatar Jimmy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.