Class: Virginia::DocumentCache

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/virginia/document_cache.rb,
lib/virginia/document_cache/document.rb

Defined Under Namespace

Classes: Document

Constant Summary collapse

NotFound =
Class.new StandardError
DEFAULT_CONTENT_TYPE =
'text/plain'
DEFAULT_LIFETIME =
10

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocumentCache



22
23
24
25
26
27
28
29
30
# File 'lib/virginia/document_cache.rb', line 22

def initialize
  @documents = {}
  @document_creators = {}

  every(60) do
    logger.debug "Reaping expired cached document"
    reap_expired!
  end
end

Class Method Details

.method_missing(m, *args, &block) ⇒ Object



17
18
19
# File 'lib/virginia/document_cache.rb', line 17

def method_missing(m, *args, &block)
  Celluloid::Actor[:virginia_document_cache].send m, *args, &block
end

Instance Method Details

#delete(id) ⇒ Object, Nil

Deletes a document from the cache



56
57
58
# File 'lib/virginia/document_cache.rb', line 56

def delete(id)
  @documents.delete id
end

#fetch(id) { ... } ⇒ Virginia::DocumentCache::Document

Retrieves a document from the cache

Yields:

  • If given, will be used to generate the document, store it, and then return.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/virginia/document_cache.rb', line 65

def fetch(id)
  # Check for a registered creator first
  check_and_run_creator id unless @documents.has_key? id

  # If we still don't have a document, check for a supplied block
  unless @documents.has_key? id

    if block_given?
      result = yield
      if result.is_a? Document
        store_document result
      else
        args = *yield
        doc = Virginia::DocumentCache::Document.new id, args[0], args[1] || DEFAULT_CONTENT_TYPE, args[2] || DEFAULT_LIFETIME
        store_document doc
      end
    else
      abort NotFound.new
    end
  end

  @documents[id]
end

#reap_expired!Object



109
110
111
112
113
# File 'lib/virginia/document_cache.rb', line 109

def reap_expired!
  @documents.each_pair do |id, doc|
    @documents.delete(id) if doc.expires_at && doc.expires_at < Time.now
  end
end

#register(id, content_type, lifetime = DEFAULT_LIFETIME, &callback) ⇒ Nil, Object

Registers a creation callback to populate a given document when requested



94
95
96
97
98
99
100
# File 'lib/virginia/document_cache.rb', line 94

def register(id, content_type, lifetime = DEFAULT_LIFETIME, &callback)
  @document_creators[id] = {
    content_type: content_type,
    lifetime: lifetime,
    callback: callback,
  }
end

#store(document, content_type = DEFAULT_CONTENT_TYPE, lifetime = DEFAULT_LIFETIME, id = nil) ⇒ String

Registers a new document with the cache



37
38
39
40
41
42
43
# File 'lib/virginia/document_cache.rb', line 37

def store(document, content_type = DEFAULT_CONTENT_TYPE, lifetime = DEFAULT_LIFETIME, id = nil)
  return if document.nil? || document.to_s.empty?

  id ||= generate_id
  doc = Virginia::DocumentCache::Document.new id, document, content_type, lifetime
  store_document doc
end

#store_document(document) ⇒ String

Registers a new Virginia::DocumentCache::Document with the cache



48
49
50
51
# File 'lib/virginia/document_cache.rb', line 48

def store_document(document)
  @documents[document.id] =  document
  document.id
end

#unregister(id) ⇒ Nil, Object

Removes a document creator registration by ID



105
106
107
# File 'lib/virginia/document_cache.rb', line 105

def unregister(id)
  @document_creators.delete id
end