Parent

Included Modules

Class Index

Quicksearch

Arrow::Cache

Instances of this class are LRU caches for disk-based objects which keep track of the cached object‘s modification time, expiring the cached version when the disk-based version changes..

Constants

DefaultConfig
Default configuration values

Attributes

extent[R]

(Not documented)

hits[R]
Total count of cache hits
list[R]
The list of cached objects
misses[R]
Total count of cache misses
name[R]
The name of the cache; used in introspection
size[R]
Cache size in bytes

Public Class Methods

new( name, config={}, &cleanup ) click to toggle source

Create a new cache. This merges the DefaultConfig with the specified values and transforms camelCased keys into under_barred ones.

    # File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 62
62:     def initialize( name, config={}, &cleanup )
63:         @name = name
64: 
65:         # Merge defaults and specified values
66:         merged = DefaultConfig.merge( config )
67: 
68:         # Transform the config hash into the form the superclass expects
69:         merged.each_key do |key|
70:             lckey = key.to_s.gsub( /(.)([A-Z])/ ) {|match|
71:                 match[0,1] + "_" + match[1,1].downcase
72:             }.intern
73: 
74:             next if key == lckey
75:             merged[ lckey ] = merged.delete( key )
76:         end
77: 
78:         # Register this instance with the class for introspection (costs
79:         # much less than ObjectSpace.each_object).
80:         obj = super( merged, &cleanup )
81:         self.class.extent << obj
82: 
83:         return obj
84:     end

Public Instance Methods

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

Overridden from the superclass to prevent .to_s from being called on objects to determine their size if the object supports a #memsize method. This is mostly to stop templates from being rendered every time they‘re cached.

     # File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 136
136:     def []=( key, obj )
137:         self.expire
138:         
139:         self.invalidate( key ) if self.cached?( key )
140: 
141:         if obj.respond_to?( :memsize )
142:             size = obj.memsize
143:         else
144:             size = obj.to_s.size
145:         end
146: 
147:         # Test against size threshold
148:         if @max_obj_size && size > @max_obj_size
149:             Arrow::Logger[self.class].debug \
150:                 "%p not cached: size exceeds maxObjSize: %d" %
151:                 [ obj, @max_obj_size ]
152:             return obj
153:         end
154:         if @max_obj_size.nil? && @max_size && size > @max_size
155:             Arrow::Logger[self.class].debug \
156:                 "%p not cached: size exceeds maxSize: %d" %
157:                 [ obj, @max_size ]
158:             return obj
159:         end
160:         
161:         if @max_num && @list.size >= @max_num
162:             Arrow::Logger[self.class].debug \
163:                 "Dropping %p from the cache: count exceeds maxNum: %d" %
164:                 [ @list.first, @max_num ]
165:             self.invalidate( @list.first )
166:         end
167: 
168:         @size += size
169:         if @max_size
170:             while @size > @max_size
171:                 Arrow::Logger[self.class].debug \
172:                     "Dropping %p from the cache: size exceeds maxSize: %d" %
173:                     [ @list.first, @max_size ]
174:                 self.invalidate( @list.first )
175:             end
176:         end
177: 
178:         @objs[ key ] = Cache::CACHE_OBJECT.new( obj, size, Time.now.to_i )
179:         @list.push( key )
180: 
181:         return obj
182:     end
expire() click to toggle source

Overridden to provide logging of expire phase.

     # File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 126
126:     def expire
127:         self.log.debug "looking for expired entries in %s" % [self.name]
128:         super
129:     end
invalidate( key ) click to toggle source

Overridden to provide logging of invalidated keys.

     # File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 112
112:     def invalidate( key )
113:         self.log.debug "invalidating cache key '%p' for %s" % [key, self.name]
114:         super
115:     end
invalidate_all() click to toggle source

Overridden for logging.

     # File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 119
119:     def invalidate_all
120:         self.log.debug "invalidating all cached objects for %s" % [self.name]
121:         super
122:     end

secsequence

--- SEC00027

seccomment

--- ""

attributes

--- 
- name: extent
  rw: R
  a_desc: ""
- name: hits
  rw: R
  a_desc: |+
    
    Total count of cache hits
    
- name: list
  rw: R
  a_desc: |+
    
    The list of cached objects
    
- name: misses
  rw: R
  a_desc: |+
    
    Total count of cache misses
    
- name: name
  rw: R
  a_desc: |+
    
    The name of the cache; used in introspection
    
- name: size
  rw: R
  a_desc: |+
    
    <a href="Cache.html">Cache</a> size in bytes
    

method_list

--- 
- methods: 
  - visibility: public
    aref: M000405
    name: new
    sourcecode: "    <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 62</span>\n\
      62:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">initialize</span>( <span class=\"ruby-identifier\">name</span>, <span class=\"ruby-identifier\">config</span>={}, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">cleanup</span> )\n\
      63:         <span class=\"ruby-ivar\">@name</span> = <span class=\"ruby-identifier\">name</span>\n\
      64: \n\
      65:         <span class=\"ruby-comment cmt\"># Merge defaults and specified values</span>\n\
      66:         <span class=\"ruby-identifier\">merged</span> = <span class=\"ruby-constant\">DefaultConfig</span>.<span class=\"ruby-identifier\">merge</span>( <span class=\"ruby-identifier\">config</span> )\n\
      67: \n\
      68:         <span class=\"ruby-comment cmt\"># Transform the config hash into the form the superclass expects</span>\n\
      69:         <span class=\"ruby-identifier\">merged</span>.<span class=\"ruby-identifier\">each_key</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">key</span><span class=\"ruby-operator\">|</span>\n\
      70:             <span class=\"ruby-identifier\">lckey</span> = <span class=\"ruby-identifier\">key</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">gsub</span>( <span class=\"ruby-regexp re\">/(.)([A-Z])/</span> ) {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">match</span><span class=\"ruby-operator\">|</span>\n\
      71:                 <span class=\"ruby-identifier\">match</span>[<span class=\"ruby-value\">0</span>,<span class=\"ruby-value\">1</span>] <span class=\"ruby-operator\">+</span> <span class=\"ruby-value str\">&quot;_&quot;</span> <span class=\"ruby-operator\">+</span> <span class=\"ruby-identifier\">match</span>[<span class=\"ruby-value\">1</span>,<span class=\"ruby-value\">1</span>].<span class=\"ruby-identifier\">downcase</span>\n\
      72:             }.<span class=\"ruby-identifier\">intern</span>\n\
      73: \n\
      74:             <span class=\"ruby-keyword kw\">next</span> <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">key</span> <span class=\"ruby-operator\">==</span> <span class=\"ruby-identifier\">lckey</span>\n\
      75:             <span class=\"ruby-identifier\">merged</span>[ <span class=\"ruby-identifier\">lckey</span> ] = <span class=\"ruby-identifier\">merged</span>.<span class=\"ruby-identifier\">delete</span>( <span class=\"ruby-identifier\">key</span> )\n\
      76:         <span class=\"ruby-keyword kw\">end</span>\n\
      77: \n\
      78:         <span class=\"ruby-comment cmt\"># Register this instance with the class for introspection (costs</span>\n\
      79:         <span class=\"ruby-comment cmt\"># much less than ObjectSpace.each_object).</span>\n\
      80:         <span class=\"ruby-identifier\">obj</span> = <span class=\"ruby-keyword kw\">super</span>( <span class=\"ruby-identifier\">merged</span>, <span class=\"ruby-operator\">&amp;</span><span class=\"ruby-identifier\">cleanup</span> )\n\
      81:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>.<span class=\"ruby-identifier\">extent</span> <span class=\"ruby-operator\">&lt;&lt;</span> <span class=\"ruby-identifier\">obj</span>\n\
      82: \n\
      83:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">obj</span>\n\
      84:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Create a <a href="Cache.html#M000405">new</a> cache. This merges the
      DefaultConfig with the specified values and transforms camelCased keys into
      under_barred ones.
      </p>
    params: ( name, config={}, &amp;cleanup )
  category: Class
  type: Public
- methods: 
  - visibility: public
    aref: M000409
    name: "[]="
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 136</span>\n\
      136:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-operator\">[]=</span>( <span class=\"ruby-identifier\">key</span>, <span class=\"ruby-identifier\">obj</span> )\n\
      137:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">expire</span>\n\
      138:         \n\
      139:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">invalidate</span>( <span class=\"ruby-identifier\">key</span> ) <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">cached?</span>( <span class=\"ruby-identifier\">key</span> )\n\
      140: \n\
      141:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">obj</span>.<span class=\"ruby-identifier\">respond_to?</span>( <span class=\"ruby-identifier\">:memsize</span> )\n\
      142:             <span class=\"ruby-identifier\">size</span> = <span class=\"ruby-identifier\">obj</span>.<span class=\"ruby-identifier\">memsize</span>\n\
      143:         <span class=\"ruby-keyword kw\">else</span>\n\
      144:             <span class=\"ruby-identifier\">size</span> = <span class=\"ruby-identifier\">obj</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">size</span>\n\
      145:         <span class=\"ruby-keyword kw\">end</span>\n\
      146: \n\
      147:         <span class=\"ruby-comment cmt\"># Test against size threshold</span>\n\
      148:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@max_obj_size</span> <span class=\"ruby-operator\">&amp;&amp;</span> <span class=\"ruby-identifier\">size</span> <span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-ivar\">@max_obj_size</span>\n\
      149:             <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">Logger</span>[<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>].<span class=\"ruby-identifier\">debug</span> \\\n\
      150:                 <span class=\"ruby-value str\">&quot;%p not cached: size exceeds maxObjSize: %d&quot;</span> <span class=\"ruby-operator\">%</span>\n\
      151:                 [ <span class=\"ruby-identifier\">obj</span>, <span class=\"ruby-ivar\">@max_obj_size</span> ]\n\
      152:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">obj</span>\n\
      153:         <span class=\"ruby-keyword kw\">end</span>\n\
      154:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@max_obj_size</span>.<span class=\"ruby-identifier\">nil?</span> <span class=\"ruby-operator\">&amp;&amp;</span> <span class=\"ruby-ivar\">@max_size</span> <span class=\"ruby-operator\">&amp;&amp;</span> <span class=\"ruby-identifier\">size</span> <span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-ivar\">@max_size</span>\n\
      155:             <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">Logger</span>[<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>].<span class=\"ruby-identifier\">debug</span> \\\n\
      156:                 <span class=\"ruby-value str\">&quot;%p not cached: size exceeds maxSize: %d&quot;</span> <span class=\"ruby-operator\">%</span>\n\
      157:                 [ <span class=\"ruby-identifier\">obj</span>, <span class=\"ruby-ivar\">@max_size</span> ]\n\
      158:             <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">obj</span>\n\
      159:         <span class=\"ruby-keyword kw\">end</span>\n\
      160:         \n\
      161:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@max_num</span> <span class=\"ruby-operator\">&amp;&amp;</span> <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">size</span> <span class=\"ruby-operator\">&gt;=</span> <span class=\"ruby-ivar\">@max_num</span>\n\
      162:             <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">Logger</span>[<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>].<span class=\"ruby-identifier\">debug</span> \\\n\
      163:                 <span class=\"ruby-value str\">&quot;Dropping %p from the cache: count exceeds maxNum: %d&quot;</span> <span class=\"ruby-operator\">%</span>\n\
      164:                 [ <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">first</span>, <span class=\"ruby-ivar\">@max_num</span> ]\n\
      165:             <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">invalidate</span>( <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">first</span> )\n\
      166:         <span class=\"ruby-keyword kw\">end</span>\n\
      167: \n\
      168:         <span class=\"ruby-ivar\">@size</span> <span class=\"ruby-operator\">+=</span> <span class=\"ruby-identifier\">size</span>\n\
      169:         <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-ivar\">@max_size</span>\n\
      170:             <span class=\"ruby-keyword kw\">while</span> <span class=\"ruby-ivar\">@size</span> <span class=\"ruby-operator\">&gt;</span> <span class=\"ruby-ivar\">@max_size</span>\n\
      171:                 <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">Logger</span>[<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">class</span>].<span class=\"ruby-identifier\">debug</span> \\\n\
      172:                     <span class=\"ruby-value str\">&quot;Dropping %p from the cache: size exceeds maxSize: %d&quot;</span> <span class=\"ruby-operator\">%</span>\n\
      173:                     [ <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">first</span>, <span class=\"ruby-ivar\">@max_size</span> ]\n\
      174:                 <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">invalidate</span>( <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">first</span> )\n\
      175:             <span class=\"ruby-keyword kw\">end</span>\n\
      176:         <span class=\"ruby-keyword kw\">end</span>\n\
      177: \n\
      178:         <span class=\"ruby-ivar\">@objs</span>[ <span class=\"ruby-identifier\">key</span> ] = <span class=\"ruby-constant\">Cache</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">CACHE_OBJECT</span>.<span class=\"ruby-identifier\">new</span>( <span class=\"ruby-identifier\">obj</span>, <span class=\"ruby-identifier\">size</span>, <span class=\"ruby-constant\">Time</span>.<span class=\"ruby-identifier\">now</span>.<span class=\"ruby-identifier\">to_i</span> )\n\
      179:         <span class=\"ruby-ivar\">@list</span>.<span class=\"ruby-identifier\">push</span>( <span class=\"ruby-identifier\">key</span> )\n\
      180: \n\
      181:         <span class=\"ruby-keyword kw\">return</span> <span class=\"ruby-identifier\">obj</span>\n\
      182:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Overridden from the superclass to prevent .to_s from being called on
      objects to determine their size if the object supports a #memsize method.
      This is mostly to stop templates from being rendered every time
      they&#8216;re cached.
      </p>
    params: ( key, obj )
  - visibility: public
    aref: M000408
    name: expire
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 126</span>\n\
      126:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">expire</span>\n\
      127:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;looking for expired entries in %s&quot;</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">name</span>]\n\
      128:         <span class=\"ruby-keyword kw\">super</span>\n\
      129:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Overridden to provide logging of <a href="Cache.html#M000408">expire</a>
      phase.
      </p>
    params: ()
  - visibility: public
    aref: M000406
    name: invalidate
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 112</span>\n\
      112:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">invalidate</span>( <span class=\"ruby-identifier\">key</span> )\n\
      113:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;invalidating cache key '%p' for %s&quot;</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-identifier\">key</span>, <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">name</span>]\n\
      114:         <span class=\"ruby-keyword kw\">super</span>\n\
      115:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Overridden to provide logging of invalidated keys.
      </p>
    params: ( key )
  - visibility: public
    aref: M000407
    name: invalidate_all
    sourcecode: "     <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/cache.rb, line 119</span>\n\
      119:     <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">invalidate_all</span>\n\
      120:         <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">log</span>.<span class=\"ruby-identifier\">debug</span> <span class=\"ruby-value str\">&quot;invalidating all cached objects for %s&quot;</span> <span class=\"ruby-operator\">%</span> [<span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">name</span>]\n\
      121:         <span class=\"ruby-keyword kw\">super</span>\n\
      122:     <span class=\"ruby-keyword kw\">end</span>"
    m_desc: |-
      <p>
      Overridden for logging.
      </p>
    params: ()
  category: Instance
  type: Public

sectitle

--- 

constants

--- 
- name: DefaultConfig
  desc: |+
    
    Default configuration values
    
  value: "{         :maxNum           =&gt; 10,         :maxObjSize       =&gt; nil,         :maxSize      =&gt; nil,         :expiration       =&gt; 3600,     }"

[Validate]

Generated with the Darkfish Rdoc Generator.