Class: IronCache::Items

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_cache/items.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Items

Returns a new instance of Items.


8
9
10
# File 'lib/iron_cache/items.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client


6
7
8
# File 'lib/iron_cache/items.rb', line 6

def client
  @client
end

Instance Method Details

#delete(key, options = {}) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/iron_cache/items.rb', line 61

def delete(key, options={})
  path2 = "#{self.path(key, options)}"
  res = @client.delete(path2)
  json = @client.parse_response(res, true)
  #return Message.new(self, res)
  return ResponseBase.new(json)
end

#get(key, options = {}) ⇒ Object

options:

:queue_name => can specify an alternative queue name
:timeout => amount of time before message goes back on the queue

19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/iron_cache/items.rb', line 19

def get(key, options={})
  begin
    res = @client.get(path(key, options), options)
    @client.logger.debug "GET response: " + res.inspect
    json = @client.parse_response(res, true)
    return Item.new(self, json)
  rescue Rest::HttpError => ex
    @client.logger.debug ex.inspect
    if ex.code == 404
      return nil
    end
    raise ex
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object

options:

:cache_name => can specify an alternative queue name

52
53
54
55
56
57
58
59
# File 'lib/iron_cache/items.rb', line 52

def increment(key, amount=1, options={})
  options = options.merge(:increment => true)
  res = @client.post(path(key, options), {:amount => amount})
  hash = @client.parse_response(res, true)
  @client.logger.debug "increment response: " + hash.inspect
  hash["key"] ||= key
  return Item.new(self, hash)
end

#path(key, options = {}) ⇒ Object


12
13
14
# File 'lib/iron_cache/items.rb', line 12

def path(key, options={})
  path = "projects/#{@client.project_id}/caches/#{CGI::escape(options[:cache_name] || @client.cache_name).gsub('+', '%20')}/items/#{CGI::escape(key).gsub('+', '%20')}#{'/increment' if options[:increment] == true}"
end

#put(key, value, options = {}) ⇒ Object

options:

:cache_name => can specify an alternative queue name
:expires_in => After this delay in seconds, message will be automatically removed from the cache.

41
42
43
44
45
46
47
48
# File 'lib/iron_cache/items.rb', line 41

def put(key, value, options={})
  to_send = options
  to_send[:value] = value
  res = @client.put(path(key, options), to_send)
  json = @client.parse_response(res, true)
  #return Message.new(self, res)
  return ResponseBase.new(json)
end

#url(key, options = {}) ⇒ Object


34
35
36
# File 'lib/iron_cache/items.rb', line 34

def url(key, options={})
  @client.url(path(key, options))
end