Class: Vedeu::Repositories::Cache Private

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

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.

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.



12
13
14
15
# File 'lib/vedeu/repositories/cache.rb', line 12

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.

Options Hash (options):

  • value (void)

    The value to be cached.

  • expires (Fixnum)

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vedeu/repositories/cache.rb', line 25

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.



40
41
42
# File 'lib/vedeu/repositories/cache.rb', line 40

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.



48
49
50
51
52
53
54
55
# File 'lib/vedeu/repositories/cache.rb', line 48

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.



61
62
63
64
65
66
67
68
69
70
# File 'lib/vedeu/repositories/cache.rb', line 61

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

  else
    false

  end
end