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_connections.rb,
lib/stockpile/cached_value_reader.rb,
lib/stockpile/cached_value_expirer.rb,
lib/stockpile/failed_lock_execution.rb,
lib/stockpile/locked_execution_result.rb,
lib/stockpile/yaml_redis_configuration.rb,
lib/stockpile/redis_connections_factory.rb,
lib/stockpile/default_redis_configuration.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, CachedValueExpirer, CachedValueReader, DefaultRedisConfiguration, RedisConnections, RedisConnectionsFactory, YamlRedisConfiguration 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.4.0'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Provides access to cache’s configuration.

Returns:



65
66
67
# File 'lib/stockpile.rb', line 65

def configuration
  @configuration ||= Configuration.new
end

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

This method returns an undefined value.

API to configure cache dynamically during runtime. Running dynamic configuration will rebuild connection pools releasing existing connections.

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



80
81
82
83
84
85
# File 'lib/stockpile.rb', line 80

def configure
  yield(configuration)
  @redis_connections = Stockpile::RedisConnectionsFactory.build_connections

  nil
end

.expire_cached(db: :default, key:) ⇒ true, false

Immediatelly expires a cached value for a given key.

Parameters:

  • db (Symbol) (defaults to: :default)

    (optional) Which Redis database to expire data from. Defaults to ‘:default`

Returns:

  • (true, false)

    Returns true if value existed in cache and was succesfully expired. Returns false if value did not exist in cache.



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

def expire_cached(db: :default, key:)
  Stockpile::CachedValueExpirer.expire_cached(db: db, key: key)
end

.perform_cached(db: :default, 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

  • db (Symbol) (defaults to: :default)

    (optional) Which Redis database to cache data in. Defaults to ‘:default`

  • 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



116
117
118
119
120
121
122
123
# File 'lib/stockpile.rb', line 116

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

.redis(db: :default) {|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



133
134
135
136
137
# File 'lib/stockpile.rb', line 133

def redis(db: :default)
  redis_connections.with(db: db) do |connection|
    yield connection
  end
end

.redis_connectionsStockpile::RedisConnections

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

Returns:



144
145
146
# File 'lib/stockpile.rb', line 144

def redis_connections
  @redis_connections ||= Stockpile::RedisConnectionsFactory.build_connections
end