Class: Scrobbler::Cache::Mongo

Inherits:
Object
  • Object
show all
Defined in:
lib/scrobbler-ng-utils/cache/mongo.rb

Overview

Caching Module which stores everything in a MongoDB Collection

Author:

  • Uwe L. Korn

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Mongo

Create a new Instance for caching Last.fmn requests.

Parameters:

  • collection (Mongo:Collecion)

    The MongoDB collection for storage



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

Parameters:

  • params (Hash) (defaults to: {})

Returns:

  • (String)


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
  options = params.merge({})
  options.delete :api_key
  options.delete 'api_key'
  @collection.find(options).each do |doc|
    if verify(doc, options) then
      found = doc['xml']
      break
    end
  end
  found
end

#has?(params = {}) ⇒ boolean

Is there already an entry for these query?

Parameters:

  • params (Hash) (defaults to: {})

    The parameters for the Last.fm Request

Returns:

  • (boolean)


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
  options = params.merge({})
  options.delete :api_key
  options.delete 'api_key'
  @collection.find(options).each do |doc|
    if verify(doc, options) then
      @hits += 1
      found = true
      break
    end
  end
  
  # We have not found a mathing cache entry.
  @misses += 1 unless found
  found
end

This method returns an undefined value.

Print statistics how efficent this cache was

Parameters:

  • prefix (String) (defaults to: "")

    The prefix which will be printed before each line



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.

Parameters:

  • data (String)
  • params (Hash) (defaults to: {})


57
58
59
60
61
62
63
64
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 57

def set(data, params={})
  options = params.merge({})
  options.delete :api_key
  options.delete 'api_key' 
  if not has?(options) then
    @collection.insert(options.merge({:xml => data, :timestamp => Time.now.to_i}))
  end
end

#verify(doc, params = {}) ⇒ boolean

Verify that a specific document matches exactly the request cache.

Parameters:

  • params (Hash) (defaults to: {})
  • doc (Hash)

Returns:

  • (boolean)


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?

Returns:

  • (boolean)


25
26
27
# File 'lib/scrobbler-ng-utils/cache/mongo.rb', line 25

def writable?
  @write
end