ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - hooopo/second_level_cache: Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6.
Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6. - hooopo/second_level_cache
Visit Site

GitHub - hooopo/second_level_cache: Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6.

GitHub - hooopo/second_level_cache: Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6.

SecondLevelCache

Gem Version build Code Climate

SecondLevelCache is a write-through and read-through caching library inspired by Cache Money and cache_fu, support ActiveRecord 4, ActiveRecord 5 and ActiveRecord 6.

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.

Install

In your gem file:

ActiveRecord 7

gem 'second_level_cache', '~> 2.7'

ActiveRecord 5.2 and 6.0:

gem 'second_level_cache', '~> 2.6.3'

ActiveRecord 5.0.x, 5.1.x:

gem 'second_level_cache', '~> 2.3.0'

For ActiveRecord 4:

gem "second_level_cache", "~> 2.1.9"

For ActiveRecord 3:

gem "second_level_cache", "~> 1.6"

Usage

For example, cache User objects:

class User < ActiveRecord::Base
  second_level_cache expires_in: 1.week
end

Then it will fetch cached object in this situations:

User.find(1)
user.articles.find(1)
User.where(status: 1).find(1)
User.where(id: 1).first # or .last
article.user

Cache key:

user = User.find(1)
user.second_level_cache_key  # We will get the key looks like "slc/user/1/0"

Expires cache:

user = User.find(1)
user.expire_second_level_cache

or expires cache using class method:

User.expire_second_level_cache(1)

Disable SecondLevelCache:

User.without_second_level_cache do
  user = User.find(1)
  # ...
end

Only SELECT * query will be cached:

# this query will NOT be cached
User.select("id, name").find(1)

Notice

  • SecondLevelCache cache by model name and id, so only find_one query will work.
  • Only equal conditions query WILL get cache; and SQL string query like User.where("name = 'Hooopo'").find(1) WILL NOT work.
  • SecondLevelCache sync cache after transaction commit:
# user and account's write_second_level_cache operation will invoke after the logger.
ActiveRecord::Base.transaction do
  user.save
  account.save
  Rails.logger.info "info"
end # <- Cache write

# if you want to do something after user and account's write_second_level_cache operation, do this way:
ActiveRecord::Base.transaction do
  user.save
  account.save
end # <- Cache write
Rails.logger.info "info"
  • If you are using SecondLevelCache with database_cleaner, you should set cleaning strategy to :truncation:
DatabaseCleaner.strategy = :truncation

Configure

In production env, we recommend to use Dalli as Rails cache store.

config.cache_store = [:dalli_store, APP_CONFIG["memcached_host"], { namespace: "ns", compress: true }]

Tips:

  • When you want to clear only second level cache apart from other cache for example fragment cache in cache store, you can only change the cache_key_prefix (default: slc):
SecondLevelCache.configure.cache_key_prefix = "slc1"
  • SecondLevelCache was added model schema digest as cache version, this means when you add/remove/change columns, the caches of this Model will expires.
  • When your want change the model cache version by manualy, just add the version option like this:
class User < ActiveRecord::Base
  second_level_cache version: 2, expires_in: 1.week
end
  • It provides a great feature, not hits db when fetching record via unique key (not primary key).
# this will fetch from cache
user = User.fetch_by_uniq_keys(nick_name: "hooopo")
post = Post.fetch_by_uniq_keys(user_id: 2, slug: "foo")

# this also fetch from cache
user = User.fetch_by_uniq_keys!(nick_name: "hooopo") # this will raise `ActiveRecord::RecordNotFound` Exception when nick name not exists.
  • You can use Rails's Eager Loading feature as normal. Even better, second_level_cache will transform the IN query into a Rails.cache.multi_read operation. For example:
Answer.includes(:question).limit(10).order("id DESC").each{|answer| answer.question.title}
Answer Load (0.2ms)  SELECT `answers`.* FROM `answers` ORDER BY id DESC LIMIT 10 # Only one SQL query and one Rails.cache.read_multi fetching operation.

Details for read_multi feature.

Original design by:

Contributors

Contributor List

License

MIT License

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory