Class: Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/libcache/cache.rb

Direct Known Subclasses

FileCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Creates a basic Cache with the UTC timezone



9
10
11
12
13
# File 'lib/libcache/cache.rb', line 9

def initialize
  ENV['TZ'] = 'UTC'
  @scheduler = Rufus::Scheduler.new
  @time_tracker = Hash.new
end

Instance Attribute Details

#expiry_timeObject

Returns the value of attribute expiry_time.



6
7
8
# File 'lib/libcache/cache.rb', line 6

def expiry_time
  @expiry_time
end

#max_sizeObject

Returns the value of attribute max_size.



6
7
8
# File 'lib/libcache/cache.rb', line 6

def max_size
  @max_size
end

#refreshObject

Returns the value of attribute refresh.



6
7
8
# File 'lib/libcache/cache.rb', line 6

def refresh
  @refresh
end

#storeObject

Returns the value of attribute store.



6
7
8
# File 'lib/libcache/cache.rb', line 6

def store
  @store
end

Instance Method Details

#check_refresh(key) ⇒ Object

Refreshes an object if it has been invalidated

Parameters:

  • key (String)

    The key value used to identify an object in the cache

Returns:

  • (Object)

    The refreshed object that is recalled from the refresh method



69
70
71
72
73
74
75
# File 'lib/libcache/cache.rb', line 69

def check_refresh(key)
  if @cache[key] == nil && !has_refresh?
    val = refresh.call(key)
    put(key, val)
    return val
  end
end

#create_storeObject

Initializes the cache store



16
17
18
# File 'lib/libcache/cache.rb', line 16

def create_store
  @cache = Hash.new
end

#exists?(key) ⇒ Boolean

Checks if a key-value pair exists in the cache

Parameters:

  • key (String)

    The key value used to identify an object in the cache

Returns:

  • (Boolean)

    The existence of the key in the cache



62
63
64
# File 'lib/libcache/cache.rb', line 62

def exists?(key)
  return @cache.key?(key)
end

#get(key) ⇒ Object

Gets the object that corresponds with the key

Parameters:

  • key (String)

    The key value used to identify an object in the cache

Returns:

  • (Object)

    The object that corresponds with the key



51
52
53
54
55
56
57
# File 'lib/libcache/cache.rb', line 51

def get(key)
  check_refresh(key)
  if(@cache[key]) == nil
    return nil
  end
  return @cache[key]
end

#get_sizeObject



82
83
84
# File 'lib/libcache/cache.rb', line 82

def get_size
  return @cache.length
end

#has_refresh?Boolean

Returns Checks if the cache has a refresh method.

Returns:

  • (Boolean)

    Checks if the cache has a refresh method



78
79
80
# File 'lib/libcache/cache.rb', line 78

def has_refresh?
  return refresh == nil
end

#invalidate(key) ⇒ Object

Deletes a key-value pair from the cache

Parameters:

  • key (String)

    The key value used to identify an object in the cache



88
89
90
# File 'lib/libcache/cache.rb', line 88

def invalidate(key)
  @cache.delete key
end

#invalidate_allObject

Clears all items in the cache



93
94
95
# File 'lib/libcache/cache.rb', line 93

def invalidate_all
  @cache.clear
end

#put(key, value) ⇒ Object

Places an object inside the cache and handles max size eviction

Parameters:

  • key (String)

    The key value used to identify an object in the cache

  • value (Object)

    The object to be placed in the cache



23
24
25
26
27
28
29
30
31
# File 'lib/libcache/cache.rb', line 23

def put(key, value)
  @cache[key] = value
  if expiry_time != nil
    @scheduler.in expiry_time, :blocking => true do
      invalidate key
    end
  end
  check_expiration(key)
end