Module: Stockpile

Defined in:
lib/stockpile.rb,
lib/stockpile/lock.rb,
lib/stockpile/cache.rb,
lib/stockpile/executor.rb,
lib/stockpile/constants.rb,
lib/stockpile/configuration.rb,
lib/stockpile/redis_connection.rb,
lib/stockpile/cached_value_reader.rb,
lib/stockpile/failed_lock_execution.rb,
lib/stockpile/locked_execution_result.rb

Overview

Copyright 2019 ConvertKit, LLC

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: Cache, CachedValueReader, RedisConnection Classes: Configuration, Executor, FailedLockExecution, Lock, LockedExcutionResult

Constant Summary collapse

DEFAULT_CONNECTION_POOL =
100
DEFAULT_CONNECTION_TIMEOUT =
3
DEFAULT_LOCK_EXPIRATION =
10
DEFAULT_REDIS_URL =
'redis://localhost:6379/1'
DEFAULT_SLUMBER =
2
DEFAULT_TTL =
60 * 5
LOCK_PREFIX =
'stockpile_lock::'
SLUMBER_COOLDOWN =
0.05
VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Provides access to cache’s configuration.

Returns:



57
58
59
# File 'lib/stockpile.rb', line 57

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ void

This method returns an undefined value.

API to configure cache dynamically during runtime.

Examples:

Configure during runtime changing redis URL

Stockpile.configure { |c| c.redis_url = 'foobar' }

Yields:

  • (configuration)

    Takes in a block of code of code that is setting or changing configuration values



70
71
72
73
# File 'lib/stockpile.rb', line 70

def configure
  yield(configuration)
  nil
end

.perform_cached(key:, ttl: Stockpile::DEFAULT_TTL) {|block| ... } ⇒ Object

Attempts to fetch a value from cache (for a given key). In case of miss will execute given block of code and cache it’s result at the provided key for a specified TTL.

Examples:

Perform cache operation

Stockpile.perform_cached(key: 'meaning_of_life', ttl: 42) { 21 * 2 }

Parameters:

  • key (String)

    Key to use for a value lookup from cache or key to store value at once it is computed

  • ttl (Integer) (defaults to: Stockpile::DEFAULT_TTL)

    (optional) Time in seconds to expire cache after. Defaults to Stockpile::DEFAULT_TTL

Yields:

  • (block)

    A block of code to be executed in case of cache miss

Returns:

  • Returns a result of block execution



90
91
92
# File 'lib/stockpile.rb', line 90

def perform_cached(key:, ttl: Stockpile::DEFAULT_TTL, &block)
  Stockpile::CachedValueReader.read_or_yield(key: key, ttl: ttl, &block)
end

.redis {|redis| ... } ⇒ Object

API to communicate with Redis database backing cache up.

Examples:

Store a value in Redis at given key

Store.redis { |r| r.set('meaning_of_life', 42) }

Yields:

Returns:

  • Returns a result of interaction with Redis



102
103
104
105
106
# File 'lib/stockpile.rb', line 102

def redis
  redis_connection_pool.with do |connection|
    yield connection
  end
end

.redis_connection_poolConnectionPool

Accessor to connection pool. Defined on top level so it can be memoized on the topmost level

Returns:

  • (ConnectionPool)

    ConnectionPool object from connection_pool gem



112
113
114
# File 'lib/stockpile.rb', line 112

def redis_connection_pool
  @redis_connection_pool ||= Stockpile::RedisConnection.connection_pool
end