Class: Scrobbler::Cache::Mongo
- Inherits:
-
Object
- Object
- Scrobbler::Cache::Mongo
- Defined in:
- lib/scrobbler-ng-utils/cache/mongo.rb
Overview
Caching Module which stores everything in a MongoDB Collection
Instance Method Summary collapse
-
#get(params = {}) ⇒ String
Get a cached result.
-
#has?(params = {}) ⇒ boolean
Is there already an entry for these query?.
-
#initialize(collection) ⇒ Mongo
constructor
Create a new Instance for caching Last.fmn requests.
-
#print_stats(prefix = "") ⇒ void
Print statistics how efficent this cache was.
-
#set(data, params = {}) ⇒ void
Store a result into the database.
-
#verify(doc, params = {}) ⇒ boolean
Verify that a specific document matches exactly the request cache.
-
#writable? ⇒ boolean
Could we write to this cache?.
Constructor Details
#initialize(collection) ⇒ Mongo
Create a new Instance for caching Last.fmn requests.
15 16 17 18 19 20 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 15 def initialize(collection) @hits = 0 @misses = 0 @collection = collection @write = true end |
Instance Method Details
#get(params = {}) ⇒ String
Get a cached result
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 70 def get(params={}) found = nil = params.merge({}) .delete :api_key .delete 'api_key' @collection.find().each do |doc| if verify(doc, ) then found = doc['xml'] break end end found end |
#has?(params = {}) ⇒ boolean
Is there already an entry for these query?
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 33 def has?(params={}) # Check if there is a matching chache entry with no extra parameters found = false = params.merge({}) .delete :api_key .delete 'api_key' @collection.find().each do |doc| if verify(doc, ) then @hits += 1 found = true break end end # We have not found a mathing cache entry. @misses += 1 unless found found end |
#print_stats(prefix = "") ⇒ void
This method returns an undefined value.
Print statistics how efficent this cache was
102 103 104 105 106 107 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 102 def print_stats(prefix="") puts prefix + "Hits: #{@hits}" puts prefix + "Misses: #{@misses}" hitrate = @hits * 1.0 / (@misses + @hits) puts prefix + "Hit-Rate: #{hitrate}" end |
#set(data, params = {}) ⇒ void
This method returns an undefined value.
Store a result into the database.
57 58 59 60 61 62 63 64 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 57 def set(data, params={}) = params.merge({}) .delete :api_key .delete 'api_key' if not has?() then @collection.insert(.merge({:xml => data, :timestamp => Time.now.to_i})) end end |
#verify(doc, params = {}) ⇒ boolean
Verify that a specific document matches exactly the request cache.
89 90 91 92 93 94 95 96 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 89 def verify(doc, params={}) matches = true doc.each do |k,v| next if ['xml', 'timestamp', '_id', 'api_key'].include?(k) matches &&= (not (params[k].nil? && params[k.to_sym].nil?)) end matches end |
#writable? ⇒ boolean
Could we write to this cache?
25 26 27 |
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 25 def writable? @write end |