Class: AsyncStorage::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/async_storage/repo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver_class, **options) ⇒ Repo

Returns a new instance of Repo.

Parameters:

  • resolver_class (Class)

    A class with the call method

  • options (Hash)

    A hash with config

  • expires_in (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)

    When the resolver_class does not respond with ‘call’ instance method



13
14
15
16
17
# File 'lib/async_storage/repo.rb', line 13

def initialize(resolver_class, **options)
  validate_resolver_class!(resolver_class)
  @resolver_class = resolver_class
  @options = options
end

Instance Attribute Details

#resolver_classObject (readonly)

Returns the value of attribute resolver_class.



7
8
9
# File 'lib/async_storage/repo.rb', line 7

def resolver_class
  @resolver_class
end

Class Method Details

.ack(klass:, args:, **options) ⇒ Hash, NilClass

Store a fresh content into redis. This method is invoked by a background job.

Parameters:

  • options (Hash)

    List of options to be passed along the initializer

  • [Class] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options

Returns:

  • (Hash, NilClass)

    the result from class resolver



25
26
27
# File 'lib/async_storage/repo.rb', line 25

def self.ack(klass:, args:, **options)
  new(klass, **options).refresh!(*args)
end

Instance Method Details

#alloc(*args) ⇒ Object

Build an Allocator instance

Parameters:

  • list (*Array)

    of parameters to be forwaded to the resolver#call



95
96
97
# File 'lib/async_storage/repo.rb', line 95

def alloc(*args)
  Allocator.new(self, *args)
end

#exist?(*args) ⇒ Boolean

Check if a fresh value exist.

Returns:

  • (Boolean)

    True or False according the object existence



74
75
76
# File 'lib/async_storage/repo.rb', line 74

def exist?(*args)
  alloc(*args).exist?
end

#expires_inObject



99
100
101
# File 'lib/async_storage/repo.rb', line 99

def expires_in
  @options[:expires_in] || AsyncStorage.config.expires_in
end

#fresh?(*args) ⇒ Boolean

Check if a fresh object exists into the storage

Returns:

  • (Boolean)

    true/false according to the object existence and freshness



88
89
90
# File 'lib/async_storage/repo.rb', line 88

def fresh?(*args)
  alloc(*args).fresh?
end

#get(*args) ⇒ Object, NilClass

Async get value with a given key

Returns:

  • (Object, NilClass)

    Return both stale or fresh object. If does not exist async call the retriever and return nil



32
33
34
# File 'lib/async_storage/repo.rb', line 32

def get(*args)
  alloc(*args).get
end

#get!(*args) ⇒ Object

Sync get value with a given value

Returns:

  • (Object)

    Return the result from resolver



39
40
41
# File 'lib/async_storage/repo.rb', line 39

def get!(*args)
  alloc(*args).get!
end

#invalidate(*args) ⇒ Boolean

Expire object the object with a given key. The stale object will not be removed

Returns:

  • (Boolean)

    True or False according to the object existence



46
47
48
# File 'lib/async_storage/repo.rb', line 46

def invalidate(*args)
  alloc(*args).invalidate
end

#invalidate!(*args) ⇒ Boolean

Delete object with a given key.

Returns:

  • (Boolean)

    True or False according to the object existence



53
54
55
# File 'lib/async_storage/repo.rb', line 53

def invalidate!(*args)
  alloc(*args).invalidate!
end

#namespaceObject



103
104
105
# File 'lib/async_storage/repo.rb', line 103

def namespace
  @options[:namespace]
end

#refresh(*args) ⇒ Object, NilClass

Invalidate object with the given key and update content according to the strategy

Returns:

  • (Object, NilClass)

    Stale object or nil when it does not exist



60
61
62
# File 'lib/async_storage/repo.rb', line 60

def refresh(*args)
  alloc(*args).refresh
end

#refresh!(*args) ⇒ Object

Fetch data from resolver and store it into redis

Returns:

  • (Object)

    Return the result from resolver



67
68
69
# File 'lib/async_storage/repo.rb', line 67

def refresh!(*args)
  alloc(*args).refresh!
end

#stale?(*args) ⇒ NilClass, Boolean

Check if object with a given key is stale

Returns:

  • (NilClass, Boolean)

    Return nil if the object does not exist or true/false according to the object freshness state



81
82
83
# File 'lib/async_storage/repo.rb', line 81

def stale?(*args)
  alloc(*args).stale?
end