Class: CloudCache

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/cloud_cache.rb

Direct Known Subclasses

ActiveSupport::Cache::CloudCache

Defined Under Namespace

Classes: CloudCacheError

Constant Summary collapse

DEFAULT_TTL =
0
DEFAULT_HOST =
"cloudcache.ws"
DEFAULT_PORT =
"80"
DEFAULT_PROTOCOL =
"http"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, options = {}) ⇒ CloudCache

Returns a new instance of CloudCache.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloud_cache.rb', line 28

def initialize(access_key, secret_key, options={})
    @access_key = access_key
    @secret_key = secret_key

    @host = options[:host] || DEFAULT_HOST
    @port = options[:port] || DEFAULT_PORT
    @protocol = options[:protocol] || DEFAULT_PROTOCOL

    @default_ttl = options[:default_ttl] || DEFAULT_TTL
    @pipeline = options[:pipeline] || false

    puts 'Creating new CloudCache [host=' + @host + ', default_ttl=' + @default_ttl.to_s + ', pipelining=' + @pipeline.to_s + ']'

end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def access_key
  @access_key
end

#default_ttlObject

Returns the value of attribute default_ttl.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def default_ttl
  @default_ttl
end

#hostObject

Returns the value of attribute host.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def host
  @host
end

#pipelineObject

Returns the value of attribute pipeline.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def pipeline
  @pipeline
end

#portObject

Returns the value of attribute port.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def protocol
  @protocol
end

#secret_keyObject

Returns the value of attribute secret_key.



26
27
28
# File 'lib/cloud_cache.rb', line 26

def secret_key
  @secret_key
end

Instance Method Details

#authObject



119
120
121
122
123
# File 'lib/cloud_cache.rb', line 119

def auth()
    command_name = "auth"
    command_path = "auth"
    run_http(:get, command_name, command_path)
end

#clearObject



247
248
249
# File 'lib/cloud_cache.rb', line 247

def clear
    flush
end

#closeObject



330
331
332
333
334
335
# File 'lib/cloud_cache.rb', line 330

def close
    # close http connection if it exists.
    if @http_conn
        @http_conn.finish
    end
end

#decrement(key, val = 1, options = {}) ⇒ Object



313
314
315
316
317
318
319
320
# File 'lib/cloud_cache.rb', line 313

def decrement(key, val=1, options={})
    headers = {"val"=>val}
    if options[:set_if_not_found]
        headers["x-cc-set-if-not-found"] = options[:set_if_not_found]
    end
    ret = run_http(:post, "POST", key + "/decr", nil, headers)
    ret.to_i
end

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cloud_cache.rb', line 262

def delete(name, options={})
    super
    begin
        run_http(:delete, "DELETE", name)
    rescue Net::HTTPServerException => ex
        puts 'CAUGHT ' + ex.response.inspect
        case ex.response
            when Net::HTTPNotFound
                return false
            else
                raise ex
        end
    end
    true
end

#delete_matched(matcher, options = nil) ⇒ Object



282
283
284
285
# File 'lib/cloud_cache.rb', line 282

def delete_matched(matcher, options = nil)
    super
    raise "delete_matched not yet supported by CloudCache"
end

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

Returns:

  • (Boolean)


287
288
289
# File 'lib/cloud_cache.rb', line 287

def exist?(key, options = nil)
    exists?(key, options)
end

#exists?(key, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


291
292
293
294
# File 'lib/cloud_cache.rb', line 291

def exists?(key, options = nil)
    x = get(key, :raw=>true)
    return !x.nil?
end

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



296
297
298
299
300
301
302
# File 'lib/cloud_cache.rb', line 296

def fetch(key, options = {})
    if (options != {})
        raise "Options on fetch() not yet supported by this library"
    end
    v = get(key)
    v
end

#flushObject



242
243
244
245
# File 'lib/cloud_cache.rb', line 242

def flush
    body = run_http(:get, "flush", "flush")
    body.strip
end

#generate_signature(service, operation, timestamp, secret_access_key) ⇒ Object



342
343
344
345
346
347
348
349
350
# File 'lib/cloud_cache.rb', line 342

def generate_signature(service, operation, timestamp, secret_access_key)
    if USE_EMBEDDED_HMAC
        my_sha_hmac = HMAC::SHA1.digest(secret_access_key, service + operation + timestamp)
    else
        my_sha_hmac = Digest::HMAC.digest(service + operation + timestamp, secret_access_key, Digest::SHA1)
    end
    my_b64_hmac_digest = Base64.encode64(my_sha_hmac).strip
    return my_b64_hmac_digest
end

#generate_timestamp(gmtime) ⇒ Object



338
339
340
# File 'lib/cloud_cache.rb', line 338

def generate_timestamp(gmtime)
    return gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")
end

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/cloud_cache.rb', line 190

def get(key, options={})
    raw = options[:raw]
    begin
        data = run_http(:get, "GET", key)
    rescue Net::HTTPServerException
        # puts $!.message
        return nil if $!.message.include? "404"
        raise $!
    end
    #puts 'data1=' + data.to_s
    if raw
        return data
    else
        #data = Base64.decode64(data)
        begin
            return Marshal.load((data))
        rescue ArgumentError => ex
            # Most likely: ArgumentError: marshal data too short
            # Let's assume the ruby version has been updated or something and handle this elegantly, next put should fix it
            puts 'ArgumentError on Marshal.load! ' + ex.message
            puts 'Returning nil, please reput and try again.'
            return nil
        end

    end
end

#get_i(key) ⇒ Object

returns the value as an int.



218
219
220
221
222
# File 'lib/cloud_cache.rb', line 218

def get_i(key)
    val = get(key, :raw=>true)
    return nil if val.nil?
    return val.to_i
end

#get_multi(keys, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cloud_cache.rb', line 141

def get_multi(keys, options={})
    return {} if keys.size == 0
    raw = options[:raw]
    kj = keys.to_json
    #puts "keys.to_json = " + kj
    extra_headers = {"keys" => kj}
    #puts "get_multi, extra_headers keys =  " + extra_headers.keys.to_s
    #puts "get_multi, extra_headers vals = " + extra_headers.values.to_s
    body = run_http(:get, "GET", "getmulti", nil, nil, extra_headers)
    #puts 'body=' + body.to_s
    # todo: should try to stream the body in
    #vals = ActiveSupport::JSON.decode body
    # New response format is:
    # VALUE <key>  <bytes> \r\n
    # <data block>\r\n
    # VALUE <key>  <bytes> \r\n
    # <data block>\r\n
    # END
    values = {}
    curr_key = nil
    data_length = 0
    val = ""
    count = 0
    body.each_line do |line|
        #print 'LINE=' + line
        if line == "END\r\n"
            # puts 'ENDED!!!'
            break
        end
        if line =~ /^VALUE (.+) (.+)/ then # (key) (bytes)
            if !curr_key.nil?
                values[curr_key] = raw ? val.strip : Marshal.load(val.strip)
            end
            curr_key, data_length = $1, $2
            val = ""
            #raise CloudCacheError, "Unexpected response #{line.inspect}"
        else
            # data block
            val += line
        end
        count += 1
    end
    if !val.nil? && val != ""
        values[curr_key] = raw ? val.strip : Marshal.load((val.strip))
    end
    #puts 'values=' + values.inspect
    values
end

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



304
305
306
307
308
309
310
311
# File 'lib/cloud_cache.rb', line 304

def increment(key, val=1, options={})
    headers = {"val"=>val}
    if options[:set_if_not_found]
        headers["x-cc-set-if-not-found"] = options[:set_if_not_found]
    end
    ret = run_http(:post, "POST", key + "/incr", nil, headers)
    ret.to_i
end

#list_keysObject



224
225
226
227
228
229
# File 'lib/cloud_cache.rb', line 224

def list_keys
    body = run_http(:get, "listkeys", "listkeys")
    # puts "list_keys=" + body
    keys = ActiveSupport::JSON.decode body # body[1..-2].split(',').collect! {|n| n.to_i}
    keys
end

#pipelined?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cloud_cache.rb', line 43

def pipelined?
    @pipeline
end

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cloud_cache.rb', line 125

def put(key, val, options={})
    seconds_to_store = options[:expires_in] || options[:ttl]
    raw = options[:raw]
    #puts 'putting ' + val.to_s + ' to key=' + key
    seconds_to_store = 0 if seconds_to_store.nil?
    if raw
        data = val.to_s
    else
        data = (Marshal.dump(val))
        #data = Base64.encode64(data)
    end
    #puts 'putting=' + data.to_s
    extra_headers = seconds_to_store > 0 ? {"ttl"=>seconds_to_store} : nil
    run_http(:put, "PUT", key, data, nil, extra_headers)
end

#read(name, options = {}) ⇒ Object



251
252
253
254
255
# File 'lib/cloud_cache.rb', line 251

def read(name, options={})
    super
    ret = get(name)
    return ret
end

#remove(name, options = nil) ⇒ Object



278
279
280
# File 'lib/cloud_cache.rb', line 278

def remove(name, options=nil)
    delete(name, options)
end

#run_http(http_method, command_name, command_path, body = nil, parameters = nil, extra_headers = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cloud_cache.rb', line 47

def run_http(http_method, command_name, command_path, body=nil, parameters=nil, extra_headers=nil)
    ts = generate_timestamp(Time.now.gmtime)
    # puts 'timestamp = ' + ts
    sig = generate_signature("CloudCache", command_name, ts, @secret_key)
    # puts "My signature = " + sig
    url = @protocol + "://" + @host + "/" + command_path # todo: append port if non standard
#         puts url

    user_agent = "CloudCache Ruby Client"
    headers = {'User-Agent' => user_agent, 'signature' => sig, 'timestamp' => ts, 'akey' => @access_key}

    if !extra_headers.nil?
        extra_headers.each_pair do |k, v|
            headers[k] = v
        end
    end

    uri = URI.parse(url)
    #puts 'body=' + body.to_s
    if (http_method == :put)
        req = Net::HTTP::Put.new(uri.path)
        req.body = body unless body.nil?
        #puts 'BODY SIZE=' + req.body.length.to_s
    elsif (http_method == :post)
        req = Net::HTTP::Post.new(uri.path)
        if !parameters.nil?
            req.set_form_data(parameters)
        end
    elsif (http_method == :delete)
        req = Net::HTTP::Delete.new(uri.path)
        if !parameters.nil?
            req.set_form_data(parameters)
        end
    else
        req = Net::HTTP::Get.new(uri.path)
        if !parameters.nil?
            req.set_form_data(parameters)
        end
    end
    headers.each_pair do |k, v|
        req[k] = v
    end
    # req.each_header do |k, v|
    # puts 'header ' + k + '=' + v
    #end
    if pipelined?
        unless @http_conn
            @http_conn = Rightscale::HttpConnection.new()
        end

        req_params =  {:request  => req,
                       :server   => @host,
                       :port     => @port,
                       :protocol => @protocol}
        res = @http_conn.request(req_params)
    else
        res = Net::HTTP.start(uri.host, uri.port) do |http|
            http.request(req)
        end
    end

#        puts 'response body=' + res.body
    case res
        when Net::HTTPSuccess
            #puts 'response body=' + res.body
            res.body
        else
            res.error!
    end

end

#shutdownObject



326
327
328
# File 'lib/cloud_cache.rb', line 326

def shutdown
    close
end

#silence!Object



322
323
324
# File 'lib/cloud_cache.rb', line 322

def silence!
    super
end

#statsObject



231
232
233
234
235
236
# File 'lib/cloud_cache.rb', line 231

def stats
    body = run_http(:get, "myusage", "myusage")
    #keys = ActiveSupport::JSON.decode body # body[1..-2].split(',').collect! {|n| n.to_i}
    #puts 'body=' + body
    body.to_i
end

#usageObject



238
239
240
# File 'lib/cloud_cache.rb', line 238

def usage
    return stats
end

#write(name, value, options = {}) ⇒ Object



257
258
259
260
# File 'lib/cloud_cache.rb', line 257

def write(name, value, options={})
    super
    put(name, value, options)
end