enabled hash-key on cache

This commit is contained in:
Parziphal 2013-12-03 22:51:09 -05:00
parent baecd199a0
commit 11d4f3772e
4 changed files with 8 additions and 16 deletions

View File

@ -6,8 +6,7 @@ trait PostCountMethods
# A small sanitation
$tags = preg_replace('/ +/', ' ', trim($tags));
$cache_version = (int)Rails::cache()->read('$cache_version');
# iTODO: cache hash key
$key = 'post_count.' . $tags . ':' . 'v.' . $cache_version;
$key = ['post_count' => $tags, 'v' => $cache_version];
$count = (int)Rails::cache()->fetch($key, function() use ($tags) {
list($sql, $params) = Post::generate_sql($tags, ['count' => true]);

View File

@ -317,8 +317,7 @@ class Tag extends Rails\ActiveRecord\Base
# * :tag_name<String>:: The tag name to search for
static public function type_name($tag_name)
{
# iTODO: hash key
return Rails::cache()->fetch('tag_type.' . $tag_name, ['expires_in' => '1 day'], function() use ($tag_name) {
return Rails::cache()->fetch(['tag_type' => $tag_name], ['expires_in' => '1 day'], function() use ($tag_name) {
return self::type_name_helper(str_replace(' ', '_', $tag_name));
});
}
@ -365,8 +364,7 @@ class Tag extends Rails\ActiveRecord\Base
$post_tags_key = [];
foreach ($post_tags as $t) {
# iTODO: hash keys.
$post_tags_key[] = 'tag_type.' . $t;
$post_tags_key[] = ['tag_type' => $t];
}
# Without this, the following splat will eat the last argument because
# it'll be considered an option instead of key (being a hash).
@ -392,8 +390,7 @@ class Tag extends Rails\ActiveRecord\Base
# Hash in format { 'name' => tag_name, 'post_count' => tag_post_count }
static public function calculate_related_by_type($tag, $type, $limit = 25)
{
# iTODO: cache key.
return Rails::cache()->fetch('category.reltags_by_type;type.' . $type . '.tag.' . $tag, ['expires_in' => '1 hour'], function() use ($tag, $type, $limit) {
return Rails::cache()->fetch(['category' => 'reltags_by_type', 'type' => $type, 'tag' => $tag], ['expires_in' => '1 hour'], function() use ($tag, $type, $limit) {
$sql = "
SELECT tags.name, tags.post_count
@ -456,8 +453,7 @@ class Tag extends Rails\ActiveRecord\Base
!is_array($tags) && $tags = array($tags);
# iTODO: hash key
return Rails::cache()->fetch('category.reltags.tags.' . implode(',', $tags), ['expires_in' => '1 hour'], function() use ($tags) {
return Rails::cache()->fetch(['category' => 'reltags', 'tags' => implode(',', $tags)], ['expires_in' => '1 hour'], function() use ($tags) {
$from = array("posts_tags pt0");
$cond = array("pt0.post_id = pt1.post_id");
$sql = "SELECT ";

View File

@ -8,8 +8,7 @@ trait CacheMethods
{
protected function update_cache()
{
# iTODO: hash keys
Rails::cache()->write('tag_type.' . $this->name, self::type_name_from_value($this->tag_type));
Rails::cache()->write(['tag_type' => $this->name], self::type_name_from_value($this->tag_type));
# Expire the tag cache if a tag's type changes.
if ($this->tag_type != $this->tagTypeWas()) {

View File

@ -54,10 +54,8 @@ class User extends Rails\ActiveRecord\Base
# iTODO: UserLog doesn't exist yet.
return;
# iTODO: hash key
return Rails::cache()->fetch('type.user_logs;id.'.$this->id.';ip.'.$ip, ['expires_in' => '10 minutes'], function() use ($ip) {
# iTODO: hash key
Rails::cache()->fetch('type.user_logs;id.all', ['expires_in' => '1 day'], function() {
return Rails::cache()->fetch(['type' => 'user_logs', 'id' => $this->id, 'ip' => $ip], ['expires_in' => '10 minutes'], function() use ($ip) {
Rails::cache()->fetch(['type' => 'user_logs', 'id' => 'all'], ['expires_in' => '1 day'], function() {
return UserLog::where('created_at < ?', date('Y-m-d 0:0:0', strtotime('-3 days')))->deleteAll();
});