CatBox 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/CatBox.rb +67 -0
- metadata +45 -0
data/lib/CatBox.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require "msgpack"
|
2
|
+
require "mysql2"
|
3
|
+
class CatBox
|
4
|
+
def initialize(scheme=nil,host="127.0.0.1",user="admin",password=nil)
|
5
|
+
options={:database => scheme,:host =>host, :username => user}
|
6
|
+
options[:password]=password if !password.nil?
|
7
|
+
@client = Mysql2::Client.new(options)
|
8
|
+
tables=@client.query( 'SHOW tables')
|
9
|
+
test=false
|
10
|
+
test=tables.each do |x|
|
11
|
+
return true if x=="cat_box"
|
12
|
+
end
|
13
|
+
build_table if !test
|
14
|
+
return true
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_table
|
18
|
+
@client.query( 'CREATE TABLE cat_box (box_key VARCHAR(80) NOT NULL PRIMARY KEY, package BLOB)')
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty_cache
|
22
|
+
@client.query( 'TRUNCATE TABLE cat_box;')
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def [](index)
|
27
|
+
fetch(index)
|
28
|
+
end
|
29
|
+
|
30
|
+
def []=(key,index)
|
31
|
+
put(key,index)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.[](index)
|
35
|
+
fetch(index)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.[]=(key,index)
|
39
|
+
put(key,index)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def put(key, data)
|
44
|
+
data=data.to_msgpack
|
45
|
+
@client.query("INSERT INTO cat_box(box_key, package) VALUES ('#{@client.escape(key.to_s)}', '#{@client.escape(data)}') ON DUPLICATE KEY UPDATE package='#{@client.escape(data)}'")
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
|
49
|
+
def fetch(key)
|
50
|
+
data=@client.query("SELECT package FROM cat_box where box_key='#{@client.escape(key.to_s)}'").first
|
51
|
+
return nil if data.nil?
|
52
|
+
data=data["package"]
|
53
|
+
return MessagePack.unpack(data)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.put(key, value)
|
57
|
+
@@default_cache.put(key, value)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.fetch(key)
|
61
|
+
@@default_cache.fetch(key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.initialize(scheme=nil,host="127.0.0.1",user="admin",password=nil)
|
65
|
+
@@default_cache=CatBox.new(scheme, host, user, password)
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: CatBox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christian Stone
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple Quick Persistant Key Value Storage in Mysql
|
15
|
+
email: nerd31337@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/CatBox.rb
|
21
|
+
homepage: http://rubygems.org/gems/catbox
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: AssCache
|
45
|
+
test_files: []
|