quick and dirty cache for popular tags result

This commit is contained in:
Kyle Drake 2016-04-18 22:25:18 -07:00
parent be6196d67c
commit 2f09415d88
4 changed files with 18 additions and 1 deletions

View file

@ -23,6 +23,14 @@ class Tag < Sequel::Model
end
def self.popular_names(limit=10)
DB["select tags.name,count(*) as c from sites_tags inner join tags on tags.id=sites_tags.tag_id where tags.name != '' and tags.is_nsfw='f' group by tags.name having count(*) > 1 order by c desc LIMIT ?", limit].all
cache = $redis_cache.get :tag_popular_names
if cache.nil?
res = DB["select tags.name,count(*) as c from sites_tags inner join tags on tags.id=sites_tags.tag_id where tags.name != '' and tags.is_nsfw='f' group by tags.name having count(*) > 1 order by c desc LIMIT ?", limit].all
$redis_cache.set :tag_popular_names, res.to_msgpack
$redis_cache.expire :tag_popular_names, 86400 # 24 hours
else
res = MessagePack.unpack cache, symbolize_keys: true
end
res
end
end