Class: MemcachedTest::Memcached

Inherits:
Object
  • Object
show all
Defined in:
lib/memcached_test/memcached.rb

Instance Method Summary collapse

Constructor Details

#initializeMemcached

Returns a new instance of Memcached.



7
8
9
# File 'lib/memcached_test/memcached.rb', line 7

def initialize
    @cache = Concurrent::Hash.new
end

Instance Method Details

#add(key, flags, exptime, bytes, data) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/memcached_test/memcached.rb', line 49

def add(key, flags, exptime, bytes, data)
    if @cache.key?(key)
        return "NOT_STORED\r\n"
    else
        item = Memcached_item.new(flags, exptime, bytes, 1, data[0, bytes.to_i])
        @cache[key] = item
        return "STORED\r\n" 
    end
end

#append(key, bytes, data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/memcached_test/memcached.rb', line 69

def append(key, bytes, data)
    if @cache.key?(key)        
        item_old = @cache[key]
        item_new = Memcached_item.new(item_old.flags, item_old.exptime, item_old.bytes.to_i + bytes.to_i, @cache[key].cas + 1, item_old.data + data[0, bytes.to_i])
        @cache[key] = item_new
        return "STORED\r\n"
    else
        return "NOT_STORED\r\n"
    end
end

#cas(key, flags, exptime, bytes, cas, data) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/memcached_test/memcached.rb', line 91

def cas(key, flags, exptime, bytes, cas, data)
    if @cache.key?(key) 
        if @cache[key].cas == cas.to_i
            item = Memcached_item.new(flags, exptime, bytes, cas.to_i + 1, data[0, bytes.to_i])
            @cache[key] = item
            return "STORED\r\n"
        else
            return "EXISTS\r\n"
        end
    else
        return "NOT_FOUND\r\n"
    end
end

#delete(key) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/memcached_test/memcached.rb', line 105

def delete(key)
    if @cache.key?(key)
        @cache.delete(key)
        return true
    else
        return nil
    end        
end

#flush_allObject



114
115
116
117
118
119
120
121
# File 'lib/memcached_test/memcached.rb', line 114

def flush_all()
    @cache.each_key do |key|
        if Time.now() > @cache[key].exptime
            self.delete(key)
        end
    end
    return "OK\r\n"
end

#get(keys) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/memcached_test/memcached.rb', line 11

def get(keys)
    result = []
    
    for key in keys do
        item = @cache[key]
        unless item.nil?
            item_array = [key, item.flags, item.exptime, item.bytes, item.data]
            result.append(item_array)
        end
    end
    return result
end

#gets(keys) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/memcached_test/memcached.rb', line 24

def gets(keys)
    result = []
    
    for key in keys do
        item = @cache[key]
        unless item.nil?
            item_array = [key, item.flags, item.exptime, item.bytes, item.cas, item.data]
            result.append(item_array)
        end
    end
    return result
end

#prepend(key, bytes, data) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/memcached_test/memcached.rb', line 80

def prepend(key, bytes, data)
    if @cache.key?(key)
        item_old = @cache[key]
        item_new = Memcached_item.new(item_old.flags, item_old.exptime, item_old.bytes.to_i + bytes.to_i, @cache[key].cas + 1, data[0, bytes.to_i] + item_old.data)
        @cache[key] = item_new
        return "STORED\r\n"
    else
        return "NOT_STORED\r\n"
    end
end

#replace(key, flags, exptime, bytes, data) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/memcached_test/memcached.rb', line 59

def replace(key, flags, exptime, bytes, data)
    if @cache.key?(key)
        item = Memcached_item.new(flags, exptime, bytes, @cache[key].cas + 1, data[0, bytes.to_i])
        @cache[key] = item
        return "STORED\r\n"
    else
        return "NOT_STORED\r\n"
    end
end

#set(key, flags, exptime, bytes, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/memcached_test/memcached.rb', line 37

def set(key, flags, exptime, bytes, data)
    if @cache.key?(key)
        item = Memcached_item.new(flags, exptime, bytes, @cache[key].cas.to_i + 1, data[0, bytes.to_i])
        @cache[key] = item
        return "STORED\r\n" 
    else
        item = Memcached_item.new(flags, exptime, bytes, 1, data[0, bytes.to_i])
        @cache[key] = item
        return "STORED\r\n"  
    end
end