Class: KNSEmailEndpoint::Storage::MemcacheStorage

Inherits:
AbstractStorage show all
Defined in:
lib/kns_email_endpoint/storage/memcache_storage.rb

Class Attribute Summary collapse

Attributes inherited from AbstractStorage

#message_id, #retry_count, #state, #unique_id

Instance Method Summary collapse

Methods inherited from AbstractStorage

#find_or_create, #to_h

Constructor Details

#initialize(settings) ⇒ MemcacheStorage

Returns a new instance of MemcacheStorage.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 9

def initialize(settings)
  options = {
    :host => "localhost",
    :port => 11211,
    :ttl => nil
  }.merge!(settings)


  # This simple bit of magic allows usage of 
  # a class connection rather than an instance connection
  # so we never have more than one active connection to memcache
  self.class.client ||= Dalli::Client.new("#{options[:host]}:#{options[:port]}")
  @client = self.class.client
  @ttl = options[:ttl]
  
end

Class Attribute Details

.clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 6

def client
  @client
end

Instance Method Details

#create(opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 26

def create(opts={})
  options = {
    :state => :unprocessed,
    :retry_count => 0
  }.merge! opts
  raise ":unique_id is required" unless options[:unique_id]
  raise ":message_id is required" unless options[:message_id]
  
  set_vars(options)
  save!
  
  return true
end

#deleteObject



51
52
53
54
55
56
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 51

def delete
  return false unless @unique_id
  @client.delete @unique_id
  reset_storage
  return true
end

#delete_allObject



58
59
60
61
62
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 58

def delete_all
  @client.flush
  reset_storage
  return true
end

#find(unique_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/kns_email_endpoint/storage/memcache_storage.rb', line 40

def find(unique_id)
  s = @client.get(unique_id)
  if s
    set_vars(s)
    return self 
  else 
    reset_storage
    return nil
  end
end