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

Returns a new instance of DocumentCache.



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

Parameters:

  • id (Object)

    ID of the document to be removed from the cache

Returns:

  • (Object, Nil)

    document Returns the document if found in the cache, nil otherwise



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

Parameters:

  • id (String)

    ID of the document to be retrieved from the cache

Yields:

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

Returns:



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

Parameters:

  • id (String)

    ID of the document for which the creator should be registered

  • content_type (String)

    Content-Type of the document

  • lifetime (Fixnum, Nil) (defaults to: DEFAULT_LIFETIME)

    The amount of time in seconds the document should be kept. If nil, document will be kept indefinitely.

Returns:

  • (Nil, Object)

    nil if the ID was not found among the creators; otherwise the creator is returned



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

Parameters:

  • document (Object)

    The document to be stored in the cache

  • lifetime (Fixnum, Nil) (defaults to: DEFAULT_LIFETIME)

    The amount of time in seconds the document should be kept. If nil, document will be kept indefinitely.

  • id (String, Nil) (defaults to: nil)

    The ID to use to store the document. If nil, one will be generated.

Returns:

  • (String)

    Cache ID of the stored document



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

Parameters:

Returns:

  • (String)

    Cache ID of the stored document



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

Parameters:

  • id (String)

    ID of the document for which the creator should be deleted

Returns:

  • (Nil, Object)

    nil if the ID was not found among the creators; otherwise the creator is returned



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

def unregister(id)
  @document_creators.delete id
end