Class: CloudCache
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- CloudCache
- Defined in:
- lib/cloud_cache.rb
Direct Known Subclasses
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
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#default_ttl ⇒ Object
Returns the value of attribute default_ttl.
-
#host ⇒ Object
Returns the value of attribute host.
-
#pipeline ⇒ Object
Returns the value of attribute pipeline.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #auth ⇒ Object
- #clear ⇒ Object
- #close ⇒ Object
- #decrement(key, val = 1, options = {}) ⇒ Object
- #delete(name, options = {}) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(key, options = nil) ⇒ Boolean
- #exists?(key, options = nil) ⇒ Boolean
- #fetch(key, options = {}) ⇒ Object
- #flush ⇒ Object
- #generate_signature(service, operation, timestamp, secret_access_key) ⇒ Object
- #generate_timestamp(gmtime) ⇒ Object
- #get(key, options = {}) ⇒ Object
-
#get_i(key) ⇒ Object
returns the value as an int.
- #get_multi(keys, options = {}) ⇒ Object
- #increment(key, val = 1, options = {}) ⇒ Object
-
#initialize(access_key, secret_key, options = {}) ⇒ CloudCache
constructor
A new instance of CloudCache.
- #list_keys ⇒ Object
- #pipelined? ⇒ Boolean
- #put(key, val, options = {}) ⇒ Object
- #read(name, options = {}) ⇒ Object
- #remove(name, options = nil) ⇒ Object
- #run_http(http_method, command_name, command_path, body = nil, parameters = nil, extra_headers = nil) ⇒ Object
- #shutdown ⇒ Object
- #silence! ⇒ Object
- #stats ⇒ Object
- #usage ⇒ Object
- #write(name, value, options = {}) ⇒ Object
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, ={}) @access_key = access_key @secret_key = secret_key @host = [:host] || DEFAULT_HOST @port = [:port] || DEFAULT_PORT @protocol = [:protocol] || DEFAULT_PROTOCOL @default_ttl = [:default_ttl] || DEFAULT_TTL @pipeline = [:pipeline] || false puts 'Creating new CloudCache [host=' + @host + ', default_ttl=' + @default_ttl.to_s + ', pipelining=' + @pipeline.to_s + ']' end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def access_key @access_key end |
#default_ttl ⇒ Object
Returns the value of attribute default_ttl.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def default_ttl @default_ttl end |
#host ⇒ Object
Returns the value of attribute host.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def host @host end |
#pipeline ⇒ Object
Returns the value of attribute pipeline.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def pipeline @pipeline end |
#port ⇒ Object
Returns the value of attribute port.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
26 27 28 |
# File 'lib/cloud_cache.rb', line 26 def protocol @protocol end |
#secret_key ⇒ Object
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
#auth ⇒ Object
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 |
#clear ⇒ Object
247 248 249 |
# File 'lib/cloud_cache.rb', line 247 def clear flush end |
#close ⇒ Object
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, ={}) headers = {"val"=>val} if [:set_if_not_found] headers["x-cc-set-if-not-found"] = [: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, ={}) 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, = nil) super raise "delete_matched not yet supported by CloudCache" end |
#exist?(key, options = nil) ⇒ Boolean
287 288 289 |
# File 'lib/cloud_cache.rb', line 287 def exist?(key, = nil) exists?(key, ) end |
#exists?(key, options = nil) ⇒ Boolean
291 292 293 294 |
# File 'lib/cloud_cache.rb', line 291 def exists?(key, = 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, = {}) if ( != {}) raise "Options on fetch() not yet supported by this library" end v = get(key) v end |
#flush ⇒ Object
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, , secret_access_key) if USE_EMBEDDED_HMAC my_sha_hmac = HMAC::SHA1.digest(secret_access_key, service + operation + ) else my_sha_hmac = Digest::HMAC.digest(service + operation + , 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 (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, ={}) raw = [:raw] begin data = run_http(:get, "GET", key) rescue Net::HTTPServerException # puts $!.message return nil if $!..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. 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, ={}) return {} if keys.size == 0 raw = [: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, ={}) headers = {"val"=>val} if [:set_if_not_found] headers["x-cc-set-if-not-found"] = [:set_if_not_found] end ret = run_http(:post, "POST", key + "/incr", nil, headers) ret.to_i end |
#list_keys ⇒ Object
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
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, ={}) seconds_to_store = [:expires_in] || [:ttl] raw = [: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, ={}) 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, =nil) delete(name, ) 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 = (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 |
#shutdown ⇒ Object
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 |
#stats ⇒ Object
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 |
#usage ⇒ Object
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, ={}) super put(name, value, ) end |