./lib/lux/cache/cache.rb in lux-fw-0.2.3 vs ./lib/lux/cache/cache.rb in lux-fw-0.5.32
- old
+ new
@@ -1,89 +1,114 @@
# frozen_string_literal: true
-module Lux::Cache
- extend self
+module Lux
+ class Cache
+ def initialize
+ @server = Lux::Cache::MemoryCache.new
+ end
- # Lux::Cache.sever = :ram
- def server= obj
- obj = "Lux::Cache::#{obj.to_s.classify}Cache".constantize if obj.class == Symbol
- @@server = obj
- end
+ # sert cache server
+ # Lux.cache.server = :memory
+ # Lux.cache.server = :memcached
+ # Lux.cache.server = Dalli::Client.new('localhost:11211', { :namespace=>Digest::MD5.hexdigest(__FILE__)[0,4], :compress => true, :expires_in => 1.hour })
+ def server= name
+ @server = if name.is_a?(Symbol)
+ if name == :memcached
+ require 'dalli'
+ Dalli::Client.new('127.0.0.1:11211', { :namespace=>Digest::MD5.hexdigest(__FILE__)[0,4], :compress => true, :expires_in => 1.hour })
+ else
+ "Lux::Cache::#{name.to_s.classify}Cache".constantize.new
+ end
+ else
+ name
+ end
- def read key
- return nil if (Lux.current.no_cache? rescue false)
- @@server.get(key)
- end
- alias :get :read
+ fetch('cache-test') { true }
+ end
- def read_multi *args
- @@server.get_multi(*args)
- end
- alias :get_multi :read_multi
+ def server
+ @server
+ end
- def write key, data, ttl=nil
- ttl = ttl.to_i if ttl
- @@server.set(key, data, ttl)
- end
- alias :set :write
+ def read key
+ return nil if (Lux.current.no_cache? rescue false)
+ @server.get(key)
+ end
+ alias :get :read
- def delete key, data=nil
- @@server.delete(key)
- end
+ def read_multi *args
+ @server.get_multi(*args)
+ end
+ alias :get_multi :read_multi
- def fetch key, opts={}
- opts = { ttl: opts } unless opts.is_a?(Hash)
- opts = opts.to_opts!(:ttl, :force, :log)
+ def write key, data, ttl=nil
+ ttl = ttl.to_i if ttl
+ @server.set(key, data, ttl)
+ end
+ alias :set :write
- opts.ttl = opts.ttl.to_i if opts.ttl
- opts.log ||= Lux.config(:log_to_stdout) unless opts.log.class == FalseClass
- opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass
+ def delete key, data=nil
+ @server.delete(key)
+ end
- @@server.delete key if opts.force
+ def fetch key, opts={}
+ opts = { ttl: opts } unless opts.is_a?(Hash)
+ opts = opts.to_opts(:ttl, :force, :log, :if)
- Lux.log " Cache.fetch.get #{key} (ttl: #{opts.ttl.or(:nil)})".green if opts.log
+ return yield if opts.if.is_a?(FalseClass)
- data = @@server.fetch key, opts.ttl do
- speed = Lux.speed { data = yield }
+ opts.ttl = opts.ttl.to_i if opts.ttl
+ opts.log ||= Lux.config(:log_to_stdout) unless opts.log.class == FalseClass
+ opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass
- Lux.log " Cache.fetch.SET #{key} len:#{data.to_s.length} (#{speed})".red if opts.log
+ @server.delete key if opts.force
+ Lux.log " Cache.fetch.get #{key} (ttl: #{opts.ttl.or(:nil)})".green if opts.log
+
+ data = @server.fetch key, opts.ttl do
+ speed = Lux.speed { data = yield }
+
+ Lux.log " Cache.fetch.SET #{key} len:#{data.to_s.length} (#{speed})".red if opts.log
+
+ data
+ end
+
data
end
- data
- end
+ def is_available?
+ set('lux-test', 9)
+ get('lux-test') == 9
+ end
- def is_available?
- set('lux-test', 9)
- get('lux-test') == 9
- end
+ def generate_key *data
+ keys = []
- def generate_key *data
- keys = []
+ for el in [data].flatten
+ keys.push el.id if el.respond_to?(:id)
- for el in [data].flatten
- keys.push el.id if el.respond_to?(:id)
-
- if el.respond_to?(:updated_at)
- keys.push el.updated_at
- elsif el.respond_to?(:created_at)
- keys.push el.created_at
- else
- keys.push el.to_s
+ if el.respond_to?(:updated_at)
+ keys.push el.updated_at
+ elsif el.respond_to?(:created_at)
+ keys.push el.created_at
+ else
+ keys.push el.to_s
+ end
end
+
+ key = keys.join('-')
+ Crypt.sha1(key)
end
- key = keys.join('-')
- Crypt.sha1(key)
- end
+ def []= key, value
+ @server.set key.to_s, value
+ value
+ end
- def []= key, value
- @@server.set key.to_s, value
- value
- end
+ def [] key
+ @server.get key.to_s
+ end
- def [] key
- @@server.get key.to_s
end
-
end
+
+require_relative 'lib/memory'