/[pupa]/ruby-cache/lib/cache.rb
ViewVC logotype

Diff of /ruby-cache/lib/cache.rb

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by okuji, Fri Nov 22 10:32:31 2002 UTC revision 1.6 by okuji, Thu Nov 28 10:16:03 2002 UTC
# Line 8  Line 8 
8  class Cache  class Cache
9    
10    CACHE_OBJECT = Struct.new('CacheObject', :content, :size, :atime)    CACHE_OBJECT = Struct.new('CacheObject', :content, :size, :atime)
11    CACHE_VERSION = '0.2'    CACHE_VERSION = '0.3'
12    
13    include Enumerable    include Enumerable
14        
15    def self.version    def self.version
16      CACHE_VERSION      CACHE_VERSION
17    end    end
18      
19    def initialize(max_obj_size = nil, max_size = nil, max_num = nil,    # initialize(max_obj_size = nil, max_size = nil, max_num = nil,
20                   expiration = nil, &hook)    #            expiration = nil, &hook)
21      if max_obj_size and max_size and max_obj_size > max_size    # initialize(hash, &hook)
22        raise ArgumentError, "max_obj_size exceeds max_size (#{max_obj_size} > #{max_size})"    def initialize(*args, &hook)
23      end      if args.size == 1 and args[0].kind_of?(Hash)
24      if max_obj_size and max_obj_size <= 0        @max_obj_size = @max_size = @max_num = @expiration = nil
25        raise ArgumentError, "invalid max_obj_size `#{max_obj_size}'"        args[0].each do |k, v|
26            k = k.intern if k.respond_to?(:intern)
27            case k
28            when :max_obj_size
29              @max_obj_size = v
30            when :max_size
31              @max_size = v
32            when :max_num
33              @max_num = v
34            when :expiration
35              @expiration = v
36            end
37          end
38        else
39          @max_obj_size, @max_size, @max_num, @expiration = args
40        end
41    
42        # Sanity checks.
43        if @max_obj_size and @max_size and @max_obj_size > @max_size
44          raise ArgumentError, "max_obj_size exceeds max_size (#{@max_obj_size} > #{@max_size})"
45        end
46        if @max_obj_size and @max_obj_size <= 0
47          raise ArgumentError, "invalid max_obj_size `#{@max_obj_size}'"
48      end      end
49      if max_size and max_size <= 0      if @max_size and @max_size <= 0
50        raise ArgumentError, "invalid max_size `#{max_size}'"        raise ArgumentError, "invalid max_size `#{@max_size}'"
51      end      end
52      if max_num and max_num <= 0      if @max_num and @max_num <= 0
53        raise ArgumentError, "invalid max_num `#{max_num}'"        raise ArgumentError, "invalid max_num `#{@max_num}'"
54      end      end
55      if expiration and expiration <= 0      if @expiration and @expiration <= 0
56        raise ArgumentError, "invalid expiration `#{expiration}'"        raise ArgumentError, "invalid expiration `#{@expiration}'"
57      end      end
58            
     @max_obj_size = max_obj_size  
     @max_size = max_size  
     @max_num = max_num  
     @expiration = expiration  
59      @hook = hook      @hook = hook
60            
61      @objs = {}      @objs = {}

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26