Class: Innate::LRUHash

### Home Classes Methods

### In Files

- lib/innate/lru_hash.rb

### Parent

Struct.new(:max_count, :expiration, :hook, :objs, :list)

### Methods

- ::new
- #[]
- #[]=
- #clear
- #delete
- #expire
- #invalidate_all

### Included Modules

- Enumerable

### Class/Module Index

Quicksearch

- Innate
- Innate::Action
- Innate::Adapter
- Innate::Cache
- Innate::Cache::API
- Innate::Cache::DRb
- Innate::Cache::FileBased
- Innate::Cache::Marshal
- Innate::Cache::Memory
- Innate::Cache::YAML
- Innate::Current
- Innate::Etanni
- Innate::Helper
- Innate::Helper::Aspect
- Innate::Helper::Aspect::SingletonMethods
- Innate::Helper::CGI
- Innate::Helper::Flash
- Innate::Helper::Link
- Innate::Helper::Redirect
- Innate::Helper::Render
- Innate::HelperAccess
- Innate::HelpersHelper
- Innate::LRUHash
- Innate::LogHub
- Innate::Mock
- Innate::Mock::Session
- Innate::Node
- Innate::Optioned
- Innate::Optioned::SingletonMethods
- Innate::Options
- Innate::Request
- Innate::Response
- Innate::Rewrite
- Innate::Route
- Innate::Session
- Innate::Session::Flash
- Innate::SingletonMethods
- Innate::StateAccessor
- Innate::Traited
- Innate::Trinity
- Innate::URLMap
- Innate::View
- Innate::View::ERB
- Innate::View::Etanni
- Innate::View::None
- Logger
- Logger::ColorFormatter
- Object

No matching classes.

# Innate::LRUHash

A Hash-alike LRU cache that provides fine-grained control over content restrictions.

It allows you to set:

- a maximum number of elements

- the maximum amount of memory used for all elements

- the allowed memory-size per element

- time to live

Differences to the original implementation include:

- The Cache is now a Struct for speed

Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org> Copyright (c) 2009 Michael Fellinger <manveru@rubyists.com>

You may redistribute it and/or modify it under the same terms as Ruby.

### Constants

CacheObject
KeyError

  On 1.8 we raise IndexError, on 1.9 we raise KeyError

### Public Class Methods

new(options = {}, &hook) click to toggle source

    # File lib/innate/lru_hash.rb, line 26
    def initialize(options = {}, &hook)
      self.max_count = options[:max_count]
      self.expiration = options[:expiration]
      self.hook = hook
      self.objs = {}
      self.list = []
    end

### Public Instance Methods

[](key) click to toggle source

    # File lib/innate/lru_hash.rb, line 63
    def [](key)
      expire

      return unless objs.key?(key)

      obj = objs[key]
      obj.atime = Time.now.to_i

      list.delete_if{|list_key| key == list_key }
      list << key

      obj.content
    end

[]=(key, obj) click to toggle source

    # File lib/innate/lru_hash.rb, line 77
    def []=(key, obj)
      expire

      delete key if objs.key?(key)

      delete list.first if max_count && max_count == list.size

      objs[key] = CacheObject.new(obj, size, Time.now.to_i)
      list << key

      obj
    end

clear() click to toggle source

    # File lib/innate/lru_hash.rb, line 46
    def clear
      objs.each{|key, obj| hook.call(key, obj) } if hook
      objs.clear
      list.clear
    end

Also aliased as: invalidate_all
delete(key) click to toggle source

    # File lib/innate/lru_hash.rb, line 34
    def delete(key)
      return unless objs.key?(key)
      obj = objs[key]

      hook.call(key, obj.content) if hook
      objs.delete key

      list.delete_if{|list_key| key == list_key }

      obj.content
    end

expire() click to toggle source

    # File lib/innate/lru_hash.rb, line 53
    def expire
      return unless expiration
      now = Time.now.to_i

      list.each_with_index do |key, index|
        break unless (objs[key].atime + expiration) <= now
        delete key
      end
    end

invalidate_all() click to toggle source
Alias for: clear

[Validate]

Generated with the Darkfish Rdoc Generator 2.