Class: Innate::Cache

Inherits:
Object
  • Object
show all
Includes:
Optioned
Defined in:
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/drb.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/api.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/yaml.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/memory.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/marshal.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache/file_based.rb

Overview

Cache manager and wrapper.

Provides a convenient wrapper around caches to keep method name confusion at a minimum while still having short and meaningful method names for every cache instance.

The default caching is specified in lib/innate.rb in the config section. At the time of writing it defaults to Innate::Cache::Memory but can be changed easily.

Configuration has to be done before Innate::setup_dependencies is being called.

Configuration:

Innate::Cache.options do |cache| cache.names = [:session, :user] cache.session = Innate::Cache::Marshal cache.user = Innate::Cache::YAML end

Usage for storing:

# Storing with a time to live (10 seconds) Innate::Cache.user.store(:manveru, "Michael Fellinger", :ttl => 10)

# Storing indefinitely Innate::Cache.user[:Pistos] = "unknown" # or without :ttl argument Innate::Cache.user.store(:Pistos, "unknown")

Usage for retrieving:

# we stored this one for 10 seconds Innate::Cache.user.fetch(:manveru, 'not here anymore') # => "Michael Fellinger" sleep 11 Innate::Cache.user.fetch(:manveru, 'not here anymore') # => "not here anymore"

Innate::Cache.user[:Pistos] # => "unknown" Innate::Cache.user.fetch(:Pistos) # => "unknown"

For more details and to find out how to implement your own cache please read the documentation of Innate::Cache::API

NOTE: * Some caches might expose their contents for everyone else on the same system, or even on connected systems. The rule as usual is, not to cache sensitive information.

Defined Under Namespace

Modules: API, FileBased Classes: DRb, Marshal, Memory, YAML

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Optioned

included, #options

Constructor Details

- (Cache) initialize(name, klass = nil)

Returns a new instance of Cache



76
77
78
79
80
81
82
83
84
85
86
87
88
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 76

def initialize(name, klass = nil)
  @name = name.to_s.dup.freeze

  klass ||= options[@name.to_sym]
  @instance = klass.new

  @instance.cache_setup(
    ENV['HOSTNAME'],
    ENV['USER'],
    'pristine',
    @name
  )
end

Instance Attribute Details

- (Object) instance (readonly)

Returns the value of attribute instance



74
75
76
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 74

def instance
  @instance
end

- (Object) name (readonly)

Returns the value of attribute name



74
75
76
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 74

def name
  @name
end

Class Method Details

+ (Object) add(*names)



111
112
113
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 111

def self.add(*names)
  names.each{|name| register(new(name)) }
end

+ (Object) register(cache)

Add accessors for cache

Parameters:



103
104
105
106
107
108
109
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 103

def self.register(cache)
  key = cache.name.to_s
  return if respond_to?(key) && respond_to?("#{key}=")
  (class << self; self; end).send(:attr_accessor, key)

  send("#{key}=", cache)
end

+ (Array) setup

Add all caches from the options.

Returns:

  • (Array)

    names of caches initialized

See Also:

  • setup_dependencies

Author:

  • manveru



96
97
98
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 96

def self.setup
  options.names.each{|name| add(name) }
end

Instance Method Details

- (Object) clear



115
116
117
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 115

def clear
  instance.cache_clear
end

- (Object) delete(*keys)



119
120
121
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 119

def delete(*keys)
  instance.cache_delete(*keys)
end

- (Object) fetch(key, default = nil) Also known as: []



123
124
125
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 123

def fetch(key, default = nil)
  instance.cache_fetch(key, default)
end

- (Object) store(key, value, options = {}) Also known as: []=



128
129
130
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/cache.rb', line 128

def store(key, value, options = {})
  instance.cache_store(key, value, options)
end