Railsã§ä»»æã®ãã£ãã·ã¥ã¹ãã¢ã移è¡ãã
SmartHR Advent Calendar 2024ã®2æ¥ç®ã§ãã
Railsã§ã¯ Rails.cache
ã§ãã£ãã·ã¥ã¹ãã¢ãæ±ããã¨ãã§ãã¾ãã
ãã®ãã£ãã·ã¥ã¹ãã¢ããä¿åå
ã®å¤æ´ããgemã®å¤æ´çã§å¥ã®ãã£ãã·ã¥ã¹ãã¢ã«ç§»è¡ãããã¨ãç¨ã«ããã¾ãã
ä»åã¯ããããªãã£ãã·ã¥ã¹ãã¢ã®ç§»è¡æé ã«ã¤ãã¦æ¸ãã¦ããã¾ãã
1. ãã¤ããªããã®ãã£ãã·ã¥ã¹ãã¢ãå®ç¾©ãã
æ¬çªç°å¢ã§ãã£ãã·ã¥ã¹ãã¢ãå¤æ´ããéã«ã¯ãæ°æ§ã®ãã£ãã·ã¥ã¹ãã¢ãä¸æ°ã«å
¥ãæ¿ããã¨ä¸å
·åãèµ·ãã¦å°ãã§ãããã
ã¾ãã¯ã移è¡æéã«ã¯åæ¹ã®ãã£ãã·ã¥ã¹ãã¢ã使ãããããªãã¤ããªããã®ãã£ãã·ã¥ã¹ãã¢ãå®ç¾©ãã¾ãã
class HybridCache < ActiveSupport::Cache::Store
def initialize(new_cache:, old_cache:)
@new_cache = new_cache
@old_cache = old_cache
end
def read(...)
old_cache.read(...) || new_cache.read(...)
end
# fetch/exist?/read_multi/fetch_multi ãåæ§ã«å®è£
ãã
def clear
old_cache.clear
new_cache.clear
end
# delete/delete_matched/write/write_multi/increment/decrement çãåæ§ã«å®è£
ãã
def silence!
old_cache.silence!
new_cache.silence!
end
def mute
old_cache.mute do
new_cache.mute do
yield
end
end
end
private
attr_reader :old_cache, :new_cache
end
config.cache_store = Hanica::HybridCache.new(
old_cache: ActiveSupport::Cache::XxxCacheStore.new(...),
new_cache: ActiveSupport::Cache::XxxCacheStore.new(...)
)
2. ãããã¤ãã
ãã¤ããªããã®ãã£ãã·ã¥ã¹ãã¢ãå®ç¾©ãããããããã¤ãã¦æ¬çªç°å¢ã§ãã£ãã·ã¥ãåæãã¦ããã¾ãã
å®éã®ã¨ãããæ°æ§ã©ã¡ãã®ãã£ãã·ã¥çµæãåªå ãããã§å®è£ ã¯è¥å¹²å¤ããã¾ãããæ°æ§ã®ãã£ãã·ã¥ã¹ãã¢ããã¾ã稼åããããã¨ã§ã移è¡æéä¸ã«æ°ãããã£ãã·ã¥ã¹ãã¢ã«ããã£ãã·ã¥ãåæ ããã¦ããããã«ãã¦ãã¾ãã
3. æ°ãããã£ãã·ã¥ã¹ãã¢ã«ç§»è¡ãã
æå¾ã«ãååã«ç§»è¡æéãçµéãã¦æ°ãããã£ãã·ã¥ã¹ãã¢ã«ãã£ãã·ã¥ãæ¸ãè¾¼ã¾ããããæ°ãããã£ãã·ã¥ã¹ãã¢ã«ç§»è¡ãã¾ãã
config.cache_store = ActiveSupport::Cache::XxxCacheStore.new(...)
ããã«ã¦ãRailsã§ä»»æã®ãã£ãã·ã¥ã¹ãã¢ã移è¡ããæé ãç´¹ä»ãã¾ããã
å®éã«ã¯redis-activesupportããRailsæ¬ä½ã®Rediså®è£
ã«ç§»è¡ããã®ã§ããããã®æé ã§åé¡ãªã移è¡ã§ãã¾ããã
以å¤ã¨ã誰ãæé ãå ¬éãã¦ããªãã£ãã®ã§åèã«ãªãã°å¹¸ãã§ãã