Created
September 11, 2018 14:57
-
-
Save dinjas/5ffe96047ba606fae2fe2339cb492108 to your computer and use it in GitHub Desktop.
Tim's cache engine code for his other app It interacts directly with memcache It is the rails side of the php caching engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Speed | |
PAGE_UPDATES = 'SPcstat:all_updates' | |
VERSION_NAME = "mv|#{Rails.env}" | |
# the master version increments the 'decimal' portion | |
# each day (using julian day) | |
# this automatically invalidates the cache at least once daily | |
def self.master_version | |
format("#{init_counter(VERSION_NAME)}.%03d", Date.current.yday) | |
end | |
def self.bump_master_version | |
$memcache.incr(VERSION_NAME) || | |
(master_version and $memcache.incr(VERSION_NAME)) | |
end | |
def self.model_name(model) | |
"#{general_name}:#{model.class.table_name}" | |
end | |
def self.general_name | |
"#{master_version}upgen:GENERAL:#{User.host}" | |
end | |
def self.model_version(model) | |
init_counter model_name(model) | |
end | |
def self.general_version | |
init_counter general_name | |
end | |
def self.init_counter(counter) | |
# create the counter and start at 1 if it does not exist | |
$memcache.incr counter, 0, 0, 1 | |
end | |
def self.page_name | |
"#{general_name}#{PAGE_UPDATES}" | |
end | |
def self.bump_general | |
$memcache.incr page_name | |
general = $memcache.incr general_name | |
return general if general | |
general_version | |
$memcache.incr page_name | |
$memcache.incr general_name | |
end | |
def self.bump_model(model) | |
cached_incr = $memcache.incr model_name(model) | |
unless cached_incr | |
model_version(model) | |
cached_incr = $memcache.incr model_name(model) | |
end | |
bump_general | |
cached_incr | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment