switch to dalli for memcache adapter, related to #1906

This commit is contained in:
r888888888 2013-08-01 15:02:45 -07:00
parent fa1b65a859
commit 60b214f4d0
7 changed files with 41 additions and 51 deletions

View File

@ -20,7 +20,8 @@ end
gem "rails", "3.2.12"
gem "pg", "0.12.2"
gem "memcache-client", :require => "memcache"
gem "kgio"
gem "dalli"
gem "delayed_job"
gem "delayed_job_active_record"
gem "simple_form"

View File

@ -82,6 +82,7 @@ GEM
coderay (1.0.9)
crack (0.3.2)
daemons (1.1.9)
dalli (2.6.4)
delayed_job (3.0.5)
activesupport (~> 3.0)
delayed_job_active_record (0.4.4)
@ -106,7 +107,6 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
memcache-client (1.8.5)
metaclass (0.0.1)
method_source (0.8.1)
mime-types (1.22)
@ -226,13 +226,14 @@ DEPENDENCIES
bcrypt-ruby
capistrano-unicorn
daemons
dalli
delayed_job
delayed_job_active_record
diff-lcs
factory_girl
ffaker!
kgio
mechanize!
memcache-client
mocha
net-sftp
net-ssh

View File

@ -32,53 +32,44 @@ class Cache
end
def self.get(key, expiry = 0)
begin
start_time = Time.now
value = MEMCACHE.get key.slice(0, 200)
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Get (%0.6f) %s -> %s' % [elapsed, key, value])
if value.nil? and block_given? then
value = yield
MEMCACHE.set key, value, expiry
end
value
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
if block_given? then
value = yield
put key, value, expiry
end
value
start_time = Time.now
value = MEMCACHE.get key
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Get (%0.6f) %s -> %s' % [elapsed, key, value])
if value.nil? and block_given? then
value = yield
MEMCACHE.set key, value, expiry
end
value
rescue => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
if block_given? then
value = yield
put key, value, expiry
end
value
end
def self.put(key, value, expiry = 0)
key.gsub!(/\s/, "_")
key = key[0, 200]
begin
start_time = Time.now
MEMCACHE.set key, value, expiry
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Set (%0.6f) %s -> %s' % [elapsed, key, value])
value
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
start_time = Time.now
MEMCACHE.set key, value, expiry
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Set (%0.6f) %s -> %s' % [elapsed, key, value])
value
rescue => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
def self.delete(key, delay = nil)
begin
start_time = Time.now
MEMCACHE.delete key, delay
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Delete (%0.6f) %s' % [elapsed, key])
nil
rescue MemCache::MemCacheError => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
start_time = Time.now
MEMCACHE.delete key, delay
elapsed = Time.now - start_time
ActiveRecord::Base.logger.debug('MemCache Delete (%0.6f) %s' % [elapsed, key])
nil
rescue => err
ActiveRecord::Base.logger.debug "MemCache Error: #{err.message}"
nil
end
def self.sanitize(key)

View File

@ -235,7 +235,7 @@ class PostPresenter < Presenter
if pool.neighbors(@post).next
@next_post_in_pool = pool.neighbors(@post).next
pool_html << template.link_to("next&thinsp;&rsaquo;".html_safe, template.post_path(pool.neighbors(@post).next, :pool_id => pool.id), :rel => next_rel, :class => "#{klass} next", :title => "to page #{pool.page_number(pool.neighbors(@post).next)}")
pool_html << template.link_to("next&thinsp;&rsaquo;".html_safe, template.post_path(@next_post_in_pool, :pool_id => pool.id), :rel => next_rel, :class => "#{klass} next", :title => "to page #{pool.page_number(@next_post_in_pool)}")
match_found = true
else
pool_html << '<span class="next">next&thinsp;&rsaquo;</span>'

View File

@ -40,7 +40,7 @@ Danbooru::Application.configure do
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
config.cache_store = :dalli_store, Danbooru.config.memcached_servers
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"

View File

@ -1,6 +0,0 @@
require 'memcache'
unless defined?(MEMCACHE)
MEMCACHE = MemCache.new :c_threshold => 10_000, :compression => true, :debug => false, :namespace => Danbooru.config.app_name.gsub(/[^A-Za-z0-9]/, "_"), :readonly => false, :urlencode => false
MEMCACHE.servers = Danbooru.config.memcached_servers
end

View File

@ -0,0 +1,3 @@
unless defined?(MEMCACHE)
MEMCACHE = Dalli::Client.new(Danbooru.config.memcached_servers, :namespace => Danbooru.config.app_name.gsub(/[^A-Za-z0-9]/, "_"))
end