Class: Google::APIClient::Service::SimpleFileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/service/simple_file_store.rb

Overview

Simple file store to be used in the event no ActiveSupport cache store is provided. This is not thread-safe, and does not support a number of features (such as expiration), but it’s useful for the simple purpose of caching discovery documents to disk. Implements the basic cache methods of ActiveSupport::Cache::Store in a limited fashion.

Instance Method Summary collapse

Constructor Details

#initialize(file_path, options = nil) ⇒ SimpleFileStore

Creates a new SimpleFileStore.

Parameters:

  • file_path (String)

    The path to the cache file on disk.

  • options (Object) (defaults to: nil)

    The options to be used with this SimpleFileStore. Not implemented.



33
34
35
# File 'lib/google/api_client/service/simple_file_store.rb', line 33

def initialize(file_path, options = nil)
  @file_path = file_path.to_s
end

Instance Method Details

#delete(name, options = nil) ⇒ Object

Deletes an entry in the cache. Returns true if an entry is deleted.

Parameters:

  • name (String)

    The name of the key. Will always be converted to a string.

  • options (Object) (defaults to: nil)

    The options to be used with this query. Not implemented.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/google/api_client/service/simple_file_store.rb', line 110

def delete(name, options = nil)
  read_file
  return nil if @cache.nil?
  if @cache.include? name.to_s
    @cache.delete name.to_s
    write_file
    return true
  else
    return nil
  end
end

#exist?(name, options = nil) ⇒ Boolean

Returns true if a key exists in the cache.

Parameters:

  • name (String)

    The name of the key. Will always be converted to a string.

  • options (Object) (defaults to: nil)

    The options to be used with this query. Not implemented.

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/google/api_client/service/simple_file_store.rb', line 43

def exist?(name, options = nil)
  read_file
  @cache.nil? ? nil : @cache.include?(name.to_s)
end

#fetch(name, options = nil) {|String| ... } ⇒ Object

Fetches data from the cache and returns it, using the given key. If the key is missing and no block is passed, returns nil. If the key is missing and a block is passed, executes the block, sets the key to its value, and returns it.

Parameters:

  • name (String)

    The name of the key. Will always be converted to a string.

  • options (Object) (defaults to: nil)

    The options to be used with this query. Not implemented.

Yields:

  • (String)

    optional block with the default value if the key is missing



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/google/api_client/service/simple_file_store.rb', line 59

def fetch(name, options = nil)
  read_file
  if block_given?
    entry = read(name.to_s, options)
    if entry.nil?
      value = yield name.to_s
      write(name.to_s, value)
      return value
    else
      return entry
    end
  else
    return read(name.to_s, options)
  end
end

#read(name, options = nil) ⇒ Object

Fetches data from the cache, using the given key. Returns nil if the key is missing.

Parameters:

  • name (String)

    The name of the key. Will always be converted to a string.

  • options (Object) (defaults to: nil)

    The options to be used with this query. Not implemented.



82
83
84
85
# File 'lib/google/api_client/service/simple_file_store.rb', line 82

def read(name, options = nil)
  read_file
  @cache.nil? ? nil : @cache[name.to_s]
end

#write(name, value, options = nil) ⇒ Object

Writes the value to the cache, with the key.

Parameters:

  • name (String)

    The name of the key. Will always be converted to a string.

  • value (Object)

    The value to be written.

  • options (Object) (defaults to: nil)

    The options to be used with this query. Not implemented.



95
96
97
98
99
100
101
# File 'lib/google/api_client/service/simple_file_store.rb', line 95

def write(name, value, options = nil)
  read_file
  @cache = {} if @cache.nil?
  @cache[name.to_s] = value
  write_file
  return nil
end