Class: GoogleReaderApi::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/google-reader-api-uniq/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/google-reader-api-uniq/cache.rb', line 4

def initialize(time)
  @time = time
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/google-reader-api-uniq/cache.rb', line 9

def [](key)
  if cached?(key)
    @hash[key].first
  else
    @hash[key] = nil
  end
end

#[]=(key, value) ⇒ Object



17
18
19
# File 'lib/google-reader-api-uniq/cache.rb', line 17

def []=(key,value)
  @hash[key] = [value,Time.now.to_i]
end

#cached?(key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/google-reader-api-uniq/cache.rb', line 21

def cached?(key)
  if @hash[key]
    Time.now.to_i - @hash[key][1] < @time
  else
    false
  end
end