Module: AsyncCache::Workers::Base

Included in:
ActiveJobWorker, SidekiqWorker
Defined in:
lib/async_cache/workers/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue_async_job(key:, version:, expires_in:, block:, arguments:) ⇒ Object

Public interface for enqueuing jobs. This is what is called by Store.

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/async_cache/workers/base.rb', line 28

def self.enqueue_async_job(key:, version:, expires_in:, block:, arguments:)
  raise NotImplementedError
end

.has_workers?Boolean

Returns whether or not workers are running to process enqueue AsyncCache jobs. Return false if this functionality isn't available by the underlying system.

Returns:

  • (Boolean)

    Returns whether or not workers are running to process enqueue AsyncCache jobs. Return false if this functionality isn't available by the underlying system.

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/async_cache/workers/base.rb', line 22

def self.has_workers?
  raise NotImplementedError
end

Instance Method Details

#backendObject (private)



51
52
53
# File 'lib/async_cache/workers/base.rb', line 51

def backend
  AsyncCache.backend
end

#perform(key, version, expires_in, block_arguments, block_source) ⇒ Object

Parameters:

  • key (String)

    String or array cache key computed by AsyncCache

  • version (Fixnum)

    Monotonically increasing integer indicating the version of the resource being cached

  • expires_in (Fixnum)

    Optional expiration to pass to the cache store

  • block_arguments (Array)

    Arguments with which to call the block

  • block_source (String)

    Ruby source to evaluate to produce the value



38
39
40
41
42
43
44
45
46
47
# File 'lib/async_cache/workers/base.rb', line 38

def perform key, version, expires_in, block_arguments, block_source
  t0 = Time.now

  _cached_data, cached_version = backend.read key
  return unless version > (cached_version || 0)

  value = [eval(block_source).call(*block_arguments), version]

  backend.write key, value, :expires_in => expires_in
end