Class: Vedeu::Repositories::Cache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/repositories/cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

This class is not currently used and will be used in future.

Allow the storing of values for a finite period to save repeated computation.

Instance Method Summary collapse

Constructor Details

#initializeVedeu::Repositories::Cache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
# File 'lib/vedeu/repositories/cache.rb', line 18

def initialize
  @cache ||= {}
  @lock  = Mutex.new
end

Instance Method Details

#add(resource, options = {}) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Add a new resource to the cache.

Parameters:

  • resource (void)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • value (void)

    The value to be cached.

  • expires (Fixnum)

    The number of seconds after which the resource will have expired.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vedeu/repositories/cache.rb', line 31

def add(resource, options = {})
  value   = options[:value]
  expires = options.fetch(:expires, 600)

  @lock.synchronize do
    @cache[resource] = {
      value:      value,
      expires_at: (Time.now + expires),
    }
  end
end

#clearHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove all cached resources.

Returns:

  • (Hash)


46
47
48
# File 'lib/vedeu/repositories/cache.rb', line 46

def clear
  @cache = {}
end

#read(resource) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Read the cached resource if it exists.

Parameters:

  • resource (void)


54
55
56
57
58
59
60
61
# File 'lib/vedeu/repositories/cache.rb', line 54

def read(resource)
  resource = @cache[resource]

  return unless resource
  return if resource[:expires_at] < Time.now

  resource[:value]
end

#remove(resource) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remove the cached resource if it exists.

Parameters:

  • resource (void)

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/vedeu/repositories/cache.rb', line 67

def remove(resource)
  if @cache.key?(resource)
    @cache.delete(resource)
    true

  else
    false

  end
end