Class: ICFS::Web::ConfigRedis

Inherits:
Config
  • Object
show all
Defined in:
lib/icfs/web/config_redis.rb

Overview

Implement Config with a Redis cache

Constant Summary

Constants inherited from Config

ICFS::Web::Config::ValConfig

Instance Attribute Summary

Attributes inherited from Config

#data, #defaults

Instance Method Summary collapse

Methods inherited from Config

#get, #set

Constructor Details

#initialize(redis, base, opts = {}) ⇒ ConfigRedis

New instance

Parameters:

  • redis (Redis)

    The redis client

  • base (Config)

    The base Config store

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :prefix (String)

    Prefix for Redis key

  • :expires (Integer)

    Expiration time in seconds



32
33
34
35
36
37
38
# File 'lib/icfs/web/config_redis.rb', line 32

def initialize(redis, base, opts={})
  super(base.defaults)
  @redis = redis
  @base = base
  @pre = opts[:prefix] || ''.freeze
  @exp = opts[:expires] || 1*60*60 # 1 hour default
end

Instance Method Details

#load(unam) ⇒ Boolean

Load a user configuration

Parameters:

  • unam (String)

    the user name to load

Returns:

  • (Boolean)

    if any config data was found for the user



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/icfs/web/config_redis.rb', line 44

def load(unam)
  Items.validate(unam, 'User/Role/Group name'.freeze, Items::FieldUsergrp)
  @unam = unam.dup
  key = _key(unam)

  # try cache
  json = @redis.get(key)
  if json
    @data = Items.parse(json, 'Config values'.freeze, Config::ValConfig)
    return true
  end

  # get base object
  succ = @base.load(unam)
  @data = @base.data
  return succ
end

#saveObject

Save a user configuration

Raises:

  • (RuntimeError)


66
67
68
69
70
71
72
# File 'lib/icfs/web/config_redis.rb', line 66

def save()
  raise(RuntimeError, 'Save requires a user name'.freeze) if !@unam
  json = Items.generate(@data, 'Config values'.freeze, Config::ValConfig)
  @redis.del(_key(@unam))
  @base.data = @data
  @base.save
end