active_redis_db 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Key < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def exists?(key)
7
+ client.exists(normalize_key(key))
8
+ end
9
+
10
+ def type?(key)
11
+ client.type(normalize_key(key))
12
+ end
13
+
14
+ def ttl?(key, format = :seconds)
15
+ normalized_key = normalize_key(key)
16
+
17
+ seconds?(format) ? client.ttl(normalized_key) : client.pttl(normalized_key)
18
+ end
19
+
20
+ def sort(key, opts = {})
21
+ client.sort(normalize_key(key), opts)
22
+ end
23
+
24
+ def sample
25
+ client.randomkey
26
+ end
27
+
28
+ def rename(key, value)
29
+ client.rename(normalize_key(key), value.to_s)
30
+ end
31
+
32
+ def rename!(key, value)
33
+ client.renamenx(normalize_key(key), value.to_s)
34
+ end
35
+
36
+ def destroy(key)
37
+ client.del(normalize_key(key))
38
+ end
39
+
40
+ def persist(key)
41
+ client.persist(normalize_key(key))
42
+ end
43
+
44
+ def expire(key, seconds, format = :seconds)
45
+ normalized_key = normalize_key(key)
46
+
47
+ if seconds?(format)
48
+ client.expire(normalized_key, seconds)
49
+ else
50
+ client.pexpire(normalized_key, seconds)
51
+ end
52
+ end
53
+
54
+ def expire_at(key, seconds, format = :seconds)
55
+ normalized_key = normalize_key(key)
56
+
57
+ if seconds?(format)
58
+ client.expireat(normalized_key, seconds)
59
+ else
60
+ client.pexpireat(normalized_key, seconds)
61
+ end
62
+ end
63
+
64
+ def dump(key)
65
+ client.dump(normalize_key(key))
66
+ end
67
+
68
+ def match(pattern = '*')
69
+ value = client.keys(normalize_key(pattern))
70
+ value = nil if value.empty?
71
+ value
72
+ end
73
+
74
+ def migrate(key, options)
75
+ client.migrate(normalize_key(key), options)
76
+ end
77
+
78
+ def move(key, destination)
79
+ client.move(normalize_key(key), destination)
80
+ end
81
+
82
+ def object(*args)
83
+ client.object(args)
84
+ end
85
+
86
+ def restore(key, milliseconds, value)
87
+ client.restore(normalize_key(key), milliseconds, value)
88
+ end
89
+
90
+ def scan(cursor, opts = {})
91
+ client.scan(cursor, opts)
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::List < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def find(key, position = 1)
7
+ value = client.lindex(normalize_key(key), (position - 1))
8
+ value = metamorph(value) if evaluate?
9
+ value
10
+ end
11
+
12
+ def first(key, limit = 1)
13
+ value = client.lrange(normalize_key(key), 0, -1)
14
+ value = (limit == 1 ? value.first : value.first(limit))
15
+ value = metamorph(value) if evaluate?
16
+ value
17
+ end
18
+
19
+ def last(key, limit = 1)
20
+ value = client.lrange(normalize_key(key), 0, -1)
21
+ value = (limit == 1 ? value.last : value.last(limit))
22
+ value = metamorph(value) if evaluate?
23
+ value
24
+ end
25
+
26
+ def between(key, start = 1, finish = 0)
27
+ value = client.lrange(normalize_key(key), (start - 1), (finish - 1))
28
+ metatransform(value)
29
+ end
30
+
31
+ def all(key)
32
+ value = client.lrange(normalize_key(key), 0, -1)
33
+ metatransform(value)
34
+ end
35
+
36
+ def count(key)
37
+ client.llen(normalize_key(key))
38
+ end
39
+
40
+ def create(key, value, order = :prepend)
41
+ normalized_key = normalize_key(key)
42
+
43
+ append?(order) ? client.rpush(normalized_key, value) : client.lpush(normalized_key, value)
44
+ end
45
+
46
+ def create!(key, value, order = :prepend)
47
+ normalized_key = normalize_key(key)
48
+
49
+ append?(order) ? client.rpushx(normalized_key, value) : client.lpushx(normalized_key, value)
50
+ end
51
+
52
+ def create_limit(key, value, limit, order = :prepend)
53
+ normalized_key = normalize_key(key)
54
+
55
+ append?(order) ? client.rpush(normalized_key, value) : client.lpush(normalized_key, value)
56
+ client.ltrim(normalized_key, 0, (limit - 1))
57
+ end
58
+
59
+ def create_limit!(key, value, limit, order = :prepend)
60
+ normalized_key = normalize_key(key)
61
+
62
+ append?(order) ? client.rpushx(normalized_key, value) : client.lpushx(normalized_key, value)
63
+ client.ltrim(normalized_key, 0, (limit - 1))
64
+ end
65
+
66
+ def create_before(key, pivot, value)
67
+ client.linsert(normalize_key(key), :before, pivot, value)
68
+ end
69
+
70
+ def create_after(key, pivot, value)
71
+ client.linsert(normalize_key(key), :after, pivot, value)
72
+ end
73
+
74
+ def update(key, index, value)
75
+ client.lset(normalize_key(key), index, value)
76
+ end
77
+
78
+ def move(key, desination)
79
+ value = client.rpoplpush(normalize_key(key), normalize_key(desination))
80
+ value = metamorph(value) if evaluate?
81
+ value
82
+ end
83
+
84
+ def move_blocking(key, desination)
85
+ value = brpoplpush(normalize_key(key), normalize_key(desination))
86
+ value = metamorph(value) if evaluate?
87
+ value
88
+ end
89
+
90
+ def destroy(key, count, value)
91
+ client.lrem(normalize_key(key), count, value)
92
+ end
93
+
94
+ def destroy_first(key, limit = 1)
95
+ client.ltrim(normalize_key(key), limit, -1)
96
+ end
97
+
98
+ def destroy_last(key, limit = 1)
99
+ client.ltrim(normalize_key(key), 0, -(limit + 1))
100
+ end
101
+
102
+ def destroy_except(key, start, finish)
103
+ client.ltrim(normalize_key(key), (start - 1), (finish - 1))
104
+ end
105
+
106
+ def destroy_all(key)
107
+ client.ltrim(normalize_key(key), -1, 0)
108
+ end
109
+
110
+ def pop(key, order = :prepend)
111
+ value = append?(order) ? client.rpop(key) : client.lpop(key)
112
+ value = metamorph(value) if evaluate?
113
+ value
114
+ end
115
+
116
+ def pop_blocking(keys, opts = {})
117
+ timeout = opts[:timeout] || 0
118
+ value = if append?(opts[:order] || :prepend)
119
+ client.brpop(keys, timeout)
120
+ else
121
+ client.blpop(keys, timeout)
122
+ end
123
+
124
+ value = metamorph(value) if evaluate?
125
+ value
126
+ end
127
+
128
+ end
129
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::PubSub < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def publish(channel, message)
7
+ client.publish(channel, message)
8
+ end
9
+
10
+ def subscribed?
11
+ client.subscribed?
12
+ end
13
+
14
+ def subscribe(*channels, &block)
15
+ client.subscribe(channels, &block)
16
+ end
17
+
18
+ def unsubscribe(*channels)
19
+ client.unsubscribe(channels)
20
+ end
21
+
22
+ def match_subscribe(*channels, &block)
23
+ client.psubscribe(channels, &block)
24
+ end
25
+
26
+ def match_unsubscribe(*channels)
27
+ client.punsubscribe(channels)
28
+ end
29
+
30
+ def state(command, *args)
31
+ client.pubsub(command, args)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Railtie < Rails::Railtie
4
+
5
+ rake_tasks do
6
+ Dir[File.expand_path('tasks/*.rake', File.dirname(__FILE__))].each { |ext| load(ext) }
7
+ end
8
+
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Script < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def script(command, *args)
7
+ client.script(command, args)
8
+ end
9
+
10
+ def eval(*args)
11
+ client.eval(:eval, args)
12
+ end
13
+
14
+ def evalsha(*args)
15
+ client.eval(:evalsha, args)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Set < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def find(key)
7
+ value = client.smembers(normalize_key(key))
8
+ metatransform(value)
9
+ end
10
+
11
+ def combine(*args)
12
+ value = client.sunion(*args)
13
+ metatransform(value)
14
+ end
15
+
16
+ def difference(*args)
17
+ value = client.sdiff(*args)
18
+ metatransform(value)
19
+ end
20
+
21
+ def intersection(key, *args)
22
+ value = client.sinter(normalize_key(key), *args)
23
+ metatransform(value)
24
+ end
25
+
26
+ def sample(key, value = 1)
27
+ value = client.srandmember(normalize_key(key), value)
28
+ metatransform(value)
29
+ end
30
+
31
+ def value?(key, value)
32
+ client.sismember(normalize_key(key), value)
33
+ end
34
+
35
+ def count(key)
36
+ client.scard(normalize_key(key))
37
+ end
38
+
39
+ def create(key, *args)
40
+ client.sadd(normalize_key(key), args)
41
+ end
42
+
43
+ def create_combination(key, *args)
44
+ client.sunionstore(normalize_key(key), args)
45
+ end
46
+
47
+ def create_difference(key, *args)
48
+ client.sdiffstore(normalize_key(key), *args)
49
+ end
50
+
51
+ def create_intersection(key, *args)
52
+ client.sinterstore(normalize_key(key), args)
53
+ end
54
+
55
+ def move(key, destination, value)
56
+ client.smove(normalize_key(key), destination.to_s, value)
57
+ end
58
+
59
+ def destroy(key, *args)
60
+ client.srem(normalize_key(key), *args)
61
+ end
62
+
63
+ def destroy_random(key)
64
+ client.spop(normalize_key(key))
65
+ end
66
+
67
+ def scan(key, cursor, opts = {})
68
+ client.sscan(normalize_key(key), cursor, opts)
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::SortedSet < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def find(key, position, opts = {})
7
+ position -= 1
8
+
9
+ value = client.zrange(normalize_key(key), position, position, opts)
10
+ value = value.first
11
+ value = metamorph(value) if evaluate?
12
+ value
13
+ end
14
+
15
+ def find_score(key, position, opts = {})
16
+ value = client.zrangebyscore(normalize_key(key), position, position, opts)
17
+ value = value.first
18
+ value = metamorph(value) if evaluate?
19
+ value
20
+ end
21
+
22
+ def first(key, opts = {})
23
+ value = client.zrange(normalize_key(key), 0, 0, opts)
24
+ value = value.first
25
+ value = metamorph(value) if evaluate?
26
+ value
27
+ end
28
+
29
+ def first_score(key, opts = {})
30
+ value = client.zrangebyscore(normalize_key(key), 1, 1, opts)
31
+ value = value.first
32
+ value = metamorph(value) if evaluate?
33
+ value
34
+ end
35
+
36
+ def last(key, opts = {})
37
+ value = client.zrevrange(normalize_key(key), 0, 0, opts)
38
+ value = value.first
39
+ value = metamorph(value) if evaluate?
40
+ value
41
+ end
42
+
43
+ def last_score(key, opts = {})
44
+ value = client.zrevrangebyscore(normalize_key(key), 1, 1, opts)
45
+ value = value.first
46
+ value = metamorph(value) if evaluate?
47
+ value
48
+ end
49
+
50
+ def between(key, start, finish, opts = {})
51
+ value = client.zrange(normalize_key(key), (start - 1), (finish - 1), opts)
52
+ value = metatransform(value)
53
+ value
54
+ end
55
+
56
+ def between_reverse(key, start, finish, opts = {})
57
+ value = client.zrevrange(normalize_key(key), (start - 1), (finish - 1), opts)
58
+ value = metatransform(value)
59
+ value
60
+ end
61
+
62
+ def between_scores(key, min, max, opts = {})
63
+ value = client.zrangebyscore(normalize_key(key), min, max, opts)
64
+ value = metatransform(value)
65
+ value
66
+ end
67
+
68
+ def between_scores_reverse(key, min, max, opts = {})
69
+ value = client.zrevrangebyscore(normalize_key(key), min, max, opts)
70
+ value = metatransform(value)
71
+ value
72
+ end
73
+
74
+ def between_lex(key, min, max, opts = {})
75
+ value = client.zrangebylex(normalize_key(key), min, max, opts)
76
+ value = metatransform(value)
77
+ value
78
+ end
79
+
80
+ def between_lex_reverse(key, min, max, opts = {})
81
+ value = client.zrevrangebylex(normalize_key(key), min, max, opts)
82
+ value = metatransform(value)
83
+ value
84
+ end
85
+
86
+ def all(key, opts = {})
87
+ value = client.zrange(normalize_key(key), 0, -1, opts)
88
+ value = metatransform(value)
89
+ value
90
+ end
91
+
92
+ def all_reverse(key, opts = {})
93
+ value = client.zrevrange(normalize_key(key), 0, -1, opts)
94
+ value = metatransform(value)
95
+ value
96
+ end
97
+
98
+ def position(key, value)
99
+ value = client.zrank(normalize_key(key), value)
100
+ value += 1 unless value.nil?
101
+ value
102
+ end
103
+
104
+ def position_reverse(key, value)
105
+ value = client.zrevrank(normalize_key(key), value)
106
+ value += 1 unless value.nil?
107
+ value
108
+ end
109
+
110
+ def score(key, value)
111
+ client.zscore(normalize_key(key), value)
112
+ end
113
+
114
+ def count(key)
115
+ client.zcard(normalize_key(key))
116
+ end
117
+
118
+ def count_between(key, min, max)
119
+ client.zcount(normalize_key(key), min, max)
120
+ end
121
+
122
+ def create(key, *args)
123
+ client.zadd(normalize_key(key), args)
124
+ end
125
+
126
+ def create_intersection(key, keys, opts = {})
127
+ client.zinterstore(normalize_key(key), keys, opts)
128
+ end
129
+
130
+ def create_combination(key, keys, opts = {})
131
+ client.zunionstore(normalize_key(key), keys, opts)
132
+ end
133
+
134
+ def increment(key, value, count)
135
+ client.zincrby(normalize_key(key), count, value)
136
+ end
137
+
138
+ def destroy(key, at)
139
+ client.zrem(normalize_key(key), at)
140
+ end
141
+
142
+ def destroy_between(key, start, finish)
143
+ client.zremrangebyrank(normalize_key(key), (start - 1), (finish - 1))
144
+ end
145
+
146
+ def destroy_scores(key, min, max)
147
+ client.zremrangebyscore(normalize_key(key), min, max)
148
+ end
149
+
150
+ def destroy_lex(key, min, max, opts = {})
151
+ client.zrevrangebylex(normalize_key(key), max, min, opts)
152
+ end
153
+
154
+ def scan(key, cursor, opts = {})
155
+ client.zscan(normalize_key(key), cursor, opts)
156
+ end
157
+
158
+ end
159
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: false
2
+
3
+ class ActiveRedisDB::String < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def find(key)
7
+ value = client.get(normalize_key(key))
8
+ value = metamorph(value) if evaluate?
9
+ value
10
+ end
11
+
12
+ def find_each(*args)
13
+ args = stringify_keys(args)
14
+ value = client.mget(args)
15
+ value = metamorph(value) if evaluate?
16
+ value
17
+ end
18
+
19
+ def length(key)
20
+ client.strlen(normalize_key(key))
21
+ end
22
+
23
+ def split(key, start, finish)
24
+ client.getrange(normalize_key(key), start, finish)
25
+ end
26
+
27
+ def create(key, value, opts = {})
28
+ client.set(normalize_key(key), value, opts)
29
+ end
30
+
31
+ def create!(key, value)
32
+ client.setnx(normalize_key(key), value)
33
+ end
34
+
35
+ def create_each(*args)
36
+ args = stringify_keys(args)
37
+ client.mset(args)
38
+ end
39
+
40
+ def create_each!(*args)
41
+ args = stringify_keys(args)
42
+ client.msetnx(args)
43
+ end
44
+
45
+ def create_until(key, value, seconds, format = :seconds)
46
+ normalized_key = normalize_key(key)
47
+
48
+ if seconds?(format)
49
+ client.setex(normalized_key, seconds, value)
50
+ else
51
+ client.psetex(normalized_key, seconds, value)
52
+ end
53
+ end
54
+
55
+ def append(key, value)
56
+ client.append(normalize_key(key), value)
57
+ end
58
+
59
+ def replace(key, value, offset)
60
+ client.setrange(normalize_key(key), offset, value)
61
+ end
62
+
63
+ def decrement(key, value = 1)
64
+ normalized_key = normalize_key(key)
65
+
66
+ value == 1 ? client.decr(normalized_key) : client.decrby(normalized_key, value)
67
+ end
68
+
69
+ def increment(key, value = 1)
70
+ normalized_key = normalize_key(key)
71
+
72
+ if value.is_a?(Float)
73
+ client.incrbyfloat(normalized_key, value)
74
+ else
75
+ value == 1 ? client.incr(normalized_key) : client.incrby(normalized_key, value)
76
+ end
77
+ end
78
+
79
+ def reset(key, value = 0)
80
+ client.getset(normalize_key(key), value)
81
+ end
82
+
83
+ def bit_count(key, start = 0, finish = -1)
84
+ client.bitcount(normalize_key(key), start, finish)
85
+ end
86
+
87
+ def bit_where(operation, key, *keys)
88
+ client.bitop(operation, key, *keys)
89
+ end
90
+
91
+ def get_bit(key, offset)
92
+ client.getbit(normalize_key(key), offset)
93
+ end
94
+
95
+ def set_bit(key, offset, bit)
96
+ client.setbit(normalize_key(key), offset, bit)
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRedisDB::Transaction < ActiveRedisDB::Base
4
+ class << self
5
+
6
+ def discard
7
+ client.discard
8
+ end
9
+
10
+ def exec
11
+ client.exec
12
+ end
13
+
14
+ def multi(&block)
15
+ client.multi(&block)
16
+ end
17
+
18
+ def watch(*args)
19
+ client.watch(args)
20
+ end
21
+
22
+ def unwatch
23
+ client.unwatch
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRedisDB
4
+ VERSION ||= '1.0.0'
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ %w[
6
+ version railtie configuration base connection geo hash hyper_log_log
7
+ key list pub_sub script set sorted_set string transaction
8
+ ].each do |file_name|
9
+ require "active_redis_db/#{file_name}"
10
+ end
11
+
12
+ require 'generators/active_redis_db/install_generator'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ class ActiveRedisDB::InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def copy_initializer_file
9
+ copy_file('install.rb', 'config/initializers/active_redis_db.rb')
10
+ end
11
+
12
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(Redis)
4
+ ActiveRedisDB.configure do |config|
5
+ config.client = Redis.new
6
+ end
7
+ end