Class: MemcachedServer::Memcache

Inherits:
Object
  • Object
show all
Defined in:
lib/memcached-server/memcache.rb

Overview

The class used to process Memcache commands and store the Memcache server data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemcache

Returns a new instance of Memcache.



14
15
16
17
18
# File 'lib/memcached-server/memcache.rb', line 14

def initialize()

    @storage = Hash.new()

end

Instance Attribute Details

#storageHash (readonly)

The Hash map used to store the Memcache server data

Returns:

  • (Hash)


12
13
14
# File 'lib/memcached-server/memcache.rb', line 12

def storage
  @storage
end

Instance Method Details

#add(key, flags, exptime, bytes, data_block) ⇒ String

Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage only if it isn’t already stored Depends on #store_item method

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



65
66
67
68
69
70
71
72
73
74
# File 'lib/memcached-server/memcache.rb', line 65

def add(key, flags, exptime, bytes, data_block)

    purge_keys()
    
    return Reply::NOT_STORED if @storage.key?(key)
    
    store_item(key, flags, exptime, bytes, data_block)
    return Reply::STORED

end

#append(key, bytes, new_data) ⇒ String

Appends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>

Parameters:

  • key (String)

    The key of the item to store

  • bytes (Integer)

    The byte size of <new_data>

  • new_data (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/memcached-server/memcache.rb', line 100

def append(key, bytes, new_data)

    purge_keys()

    return Reply::NOT_STORED unless @storage.key?(key)

    item = @storage[key]
    item.lock.synchronize do

        item.data_block.concat(new_data)
        item.bytes += bytes

    end

    item.update_cas_id()
    return Reply::STORED

end

#cas(key, flags, exptime, bytes, cas_id, data_block) ⇒ String

Check and set operation that stores a MemcachedServer::Item only if no one else has updated since it was last fetched Depends on #store_item method

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

  • cas_id (Integer)

    Is a unique integer value

Returns:

  • (String)

    The reply that describes the result of the operation



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/memcached-server/memcache.rb', line 154

def cas(key, flags, exptime, bytes, cas_id, data_block)

    purge_keys()

    return Reply::NOT_FOUND unless @storage.key?(key)

    item = @storage[key]
    item.lock.synchronize do

        return Reply::EXISTS if cas_id != item.cas_id

    end

    store_item(key, flags, exptime, bytes, data_block)
    return Reply::STORED

end

#get(keys) ⇒ [MemcachedServer::Item]

Retrieves the items corresponding to the given keys from @storage

Parameters:

  • keys ([String])

    Array that contains the keys of the items to retrieve

Returns:



31
32
33
34
35
36
37
38
# File 'lib/memcached-server/memcache.rb', line 31

def get(keys)

    purge_keys()

    items = @storage.values_at(*keys)
    return items

end

#prepend(key, bytes, new_data) ⇒ String

Prepends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>

Parameters:

  • key (String)

    The key of the item to store

  • bytes (Integer)

    The byte size of <new_data>

  • new_data (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/memcached-server/memcache.rb', line 125

def prepend(key, bytes, new_data)

    purge_keys()

    return Reply::NOT_STORED unless @storage.key?(key)

    item = @storage[key]
    item.lock.synchronize do 

        item.data_block.prepend(new_data)
        item.bytes += bytes

    end

    item.update_cas_id()
    return Reply::STORED

end

#purge_keysObject

Deletes @storage items if they are expired



21
22
23
24
25
# File 'lib/memcached-server/memcache.rb', line 21

def purge_keys()

    @storage.delete_if { | key, item | item.expired? }

end

#replace(key, flags, exptime, bytes, data_block) ⇒ String

Replaces a MemcachedServer::Item stored in @storage, with a new one with the attributes recieved by param Depends on #store_item method

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



85
86
87
88
89
90
91
92
# File 'lib/memcached-server/memcache.rb', line 85

def replace(key, flags, exptime, bytes, data_block)
    
    return Reply::NOT_STORED unless @storage.key?(key)

    store_item(key, flags, exptime, bytes, data_block)
    return Reply::STORED

end

#set(key, flags, exptime, bytes, data_block) ⇒ String

Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Depends on #store_item method

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>

Returns:

  • (String)

    The reply that describes the result of the operation



49
50
51
52
53
54
# File 'lib/memcached-server/memcache.rb', line 49

def set(key, flags, exptime, bytes, data_block)

    store_item(key, flags, exptime, bytes, data_block)
    return Reply::STORED

end

#store_item(key, flags, exptime, bytes, data_block) ⇒ Object

Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Before storing the MemcachedServer::Item it updates it’s cas_id

Parameters:

  • key (String)

    The key of the item to store

  • flags (Integer)

    Is an arbitrary unsigned integer (written out in decimal)

  • exptime (Integer)

    The exptime of the Item to store

  • bytes (Integer)

    The byte size of <data_block>

  • data_block (String)

    Is a chunk of arbitrary 8-bit data of length <bytes>



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/memcached-server/memcache.rb', line 180

def store_item(key, flags, exptime, bytes, data_block)

    item = Item.new(key, flags, exptime, bytes, data_block)
    item.update_cas_id()
    item.lock.synchronize do
        
        @storage.store(key, item) unless item.expired?()

    end

end