redisk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,132 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ # dir ./tmp/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
116
+
117
+ # Use object sharing. Can save a lot of memory if you have many common
118
+ # string in your dataset, but performs lookups against the shared objects
119
+ # pool so it uses more CPU and can be a bit slower. Usually it's a good
120
+ # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
131
+ shareobjects no
132
+ shareobjectspoolsize 1024
@@ -0,0 +1,348 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Redisk::IO do
4
+ before do
5
+ # load a file into an IO object
6
+ @io_name = 'rails.log'
7
+ @key = Redisk::IO.list_key(@io_name)
8
+ @file_as_array = []
9
+ File.foreach(File.dirname(__FILE__) + '/fixtures/rails.log') {|f|
10
+ @file_as_array << f
11
+ Redisk.redis.rpush @key, f
12
+ }
13
+ @io = Redisk::IO.new(@io_name)
14
+ end
15
+
16
+ after do
17
+ Redisk.redis.del @key
18
+ end
19
+
20
+ describe 'new' do
21
+ it 'returns an IO object' do
22
+ @io.should be_instance_of(Redisk::IO)
23
+ end
24
+
25
+ it 'connects to the redis server' do
26
+ @io.redis.should be_instance_of(Redis::Namespace)
27
+ end
28
+
29
+ it 'stores the name of the key' do
30
+ @io.name.should == @io_name
31
+ end
32
+ end
33
+
34
+ describe 'foreach' do
35
+
36
+ it 'should yield each line to the block' do
37
+ Redisk::IO.foreach(@io_name) do |line|
38
+ line.should be_kind_of(String)
39
+ end
40
+ end
41
+
42
+ it 'should return nil' do
43
+ Redisk::IO.foreach(@io_name) {|l| l }.should == nil
44
+ end
45
+
46
+ end
47
+
48
+ describe 'open' do
49
+
50
+ it 'should return an IO object if no block is given' do
51
+ Redisk::IO.open('newlog').should be_kind_of(Redisk::IO)
52
+ end
53
+
54
+ it 'should yield the IO object to the block' do
55
+ Redisk::IO.open('newlog') do |log|
56
+ log.should be_kind_of(Redisk::IO)
57
+ end
58
+ end
59
+
60
+ it 'should return the value of the block' do
61
+ Redisk::IO.open('newlog') do |log|
62
+ 'returned'
63
+ end.should == 'returned'
64
+ end
65
+
66
+ end
67
+
68
+ describe 'read' do
69
+
70
+ it 'should return the entire contents without arguments' do
71
+ Redisk::IO.read(@io_name).should == @file_as_array.join("\n")
72
+ end
73
+
74
+ it 'should return the first [length] contents' do
75
+ Redisk::IO.read(@io_name, 2).should == @file_as_array[0..1].join("\n")
76
+ end
77
+
78
+ it 'should return [length] contents starting at [offset]' do
79
+ Redisk::IO.read(@io_name, 2, 2).should == @file_as_array[2..3].join("\n")
80
+ end
81
+
82
+ end
83
+
84
+ describe 'readlines' do
85
+
86
+ it 'should return the entire contents as an array' do
87
+ Redisk::IO.readlines(@io_name).should == @file_as_array
88
+ end
89
+
90
+ end
91
+
92
+ describe 'instance methods' do
93
+ before(:each) do
94
+ @io = Redisk::IO.new(@io_name)
95
+ end
96
+
97
+ describe '#<<' do
98
+
99
+ it 'should append a line to the io' do
100
+ @io << 'whu'
101
+ @io.lineno = 100
102
+ @io.gets.should == 'whu'
103
+ end
104
+
105
+ it 'should return the text' do
106
+ (@io << 'whu').should == 'whu'
107
+ end
108
+
109
+ it 'should be able to chain' do
110
+ @io << 'whu ' << 'zuh'
111
+ @io.lineno = 100
112
+ @io.gets.should == 'whu zuh'
113
+ end
114
+
115
+ end
116
+
117
+ describe '#each' do
118
+
119
+ it 'should yield each line of the file' do
120
+ i = 0
121
+ @io.each {|l|
122
+ l.should be_kind_of(String)
123
+ l.should == @file_as_array[i]
124
+ i+=1
125
+ }
126
+ i.should == 100
127
+ end
128
+
129
+ it 'should advance the lineno for each iteration' do
130
+ i = 0
131
+ @io.each {|l|
132
+ i+=1
133
+ @io.lineno.should == i
134
+ }
135
+ @io.lineno.should == 100
136
+ end
137
+
138
+ end
139
+
140
+ describe '#eof' do
141
+
142
+ it 'should return false if lineno is not the end of the file' do
143
+ @io.eof.should == false
144
+ end
145
+
146
+ it 'should return true if lineno is at the end of the file' do
147
+ @io.each {|l| l }
148
+ @io.eof.should == true
149
+ end
150
+
151
+ end
152
+
153
+ describe '#gets' do
154
+
155
+ it 'should return the next line from the io' do
156
+ val = @io.gets
157
+ val.should be_kind_of(String)
158
+ val.should == @file_as_array[0]
159
+ end
160
+
161
+ it 'should advance the filedescriptor lineno' do
162
+ @io.gets.should == @file_as_array[0]
163
+ @io.lineno.should == 1
164
+ @io.gets.should == @file_as_array[1]
165
+ @io.lineno.should == 2
166
+ end
167
+
168
+ it 'should set the value of $_' do
169
+ val = @io.gets
170
+ @io._.should == val
171
+ end
172
+
173
+ end
174
+
175
+ describe '#lineno' do
176
+ it 'should return the current lineno' do
177
+ @io.lineno.should == 0
178
+ end
179
+ end
180
+
181
+ describe '#lineno=' do
182
+ it 'should set the current lineno' do
183
+ @io.lineno = 50
184
+ @io.lineno.should == 50
185
+ @io.gets.should == @file_as_array[50]
186
+ end
187
+ end
188
+
189
+ describe '#print' do
190
+
191
+ it 'should append arguments into a string and write to io' do
192
+ @io.print('My ', 'name ', 'is ', 'redisk')
193
+ line = Redisk::IO.read(@io_name, 1, 100)
194
+ line.should == 'My name is redisk'
195
+ end
196
+
197
+ it 'should return nil' do
198
+ @io.print('whu').should == nil
199
+ end
200
+
201
+ it 'should write the contents of $_ if no arguments are provided' do
202
+ @io.gets
203
+ @io.print
204
+ line = Redisk::IO.read(@io_name, 1, 100)
205
+ line.should == @file_as_array[0]
206
+ end
207
+ end
208
+
209
+ describe '#printf' do
210
+
211
+ it 'should run the arguments through sprintf and print to the io' do
212
+ @io.printf('My name is %s', 'redisk')
213
+ line = Redisk::IO.read(@io_name, 1, 100)
214
+ line.should == 'My name is redisk'
215
+ end
216
+
217
+ end
218
+
219
+ describe '#puts' do
220
+
221
+ it 'should write each argument to the io' do
222
+ @io.puts('1', '2', '3')
223
+ @io.lineno = 100
224
+ @io.gets.should == '1'
225
+ @io.gets.should == '2'
226
+ @io.gets.should == '3'
227
+ end
228
+
229
+ it 'should write a blank string if no argument is passed' do
230
+ @io.puts
231
+ @io.lineno = 100
232
+ @io.gets.should == ''
233
+ end
234
+
235
+ end
236
+
237
+ describe '#readline' do
238
+
239
+ it 'should read the next line with gets' do
240
+ @io.readline.should == @file_as_array[0]
241
+ @io.lineno.should == 1
242
+ end
243
+
244
+ it 'should raise EOFError at the end of the lines' do
245
+ @io.lineno = 100
246
+ lambda {
247
+ @io.readline
248
+ }.should raise_error(Redisk::IO::EOFError)
249
+ end
250
+
251
+ end
252
+
253
+ describe '#readlines' do
254
+
255
+ it 'should return the lines as an array' do
256
+ @io.readlines.should == @file_as_array
257
+ end
258
+
259
+ end
260
+
261
+ describe '#rewind' do
262
+
263
+ it 'should set the lineno to 0' do
264
+ @io.lineno = 50
265
+ @io.rewind.should == 0
266
+ @io.lineno.should == 0
267
+ end
268
+
269
+ end
270
+
271
+ describe '#seek' do
272
+
273
+ it 'should set the lineno to the absolute location' do
274
+ @io.seek(10)
275
+ @io.lineno.should == 10
276
+ end
277
+
278
+ it 'should set the lineno to the absolute location with SEEK_SET' do
279
+ @io.seek(15, IO::SEEK_SET)
280
+ @io.lineno.should == 15
281
+ end
282
+
283
+ it 'should set the lineno to the location relative to the end with SEEK_END' do
284
+ @io.seek(-5, IO::SEEK_END)
285
+ @io.lineno.should == 95
286
+ end
287
+
288
+ it 'should set the lineno to an offset position from the current line with SEEK_CUR' do
289
+ @io.lineno = 5
290
+ @io.lineno.should == 5
291
+ @io.seek(5, IO::SEEK_CUR)
292
+ @io.lineno.should == 10
293
+ end
294
+
295
+ end
296
+
297
+ describe '#stat' do
298
+
299
+ it 'should return a stat like object' do
300
+ @stat = @io.stat
301
+ @stat.should be_instance_of(Redisk::Stat)
302
+ @stat.atime.should be_instance_of(Time)
303
+ @stat.size.should == 0
304
+ end
305
+
306
+ end
307
+
308
+ describe '#write' do
309
+
310
+ it 'should write the contents of string to the io' do
311
+ @io.write('123')
312
+ @io.lineno = 100
313
+ @io.gets.should == '123'
314
+ end
315
+
316
+ it 'should return the number of bytes written' do
317
+ @io.write('123').should == 3
318
+ end
319
+
320
+ end
321
+
322
+ describe '#truncate(size)' do
323
+
324
+ it 'should only keep the last [size] lines' do
325
+ @io.truncate(10).should == 10
326
+ @io.length.should == 10
327
+ @io.readlines.should == @file_as_array[90...100]
328
+ end
329
+
330
+ end
331
+
332
+ end
333
+
334
+ describe 'As an interface for logger' do
335
+ before do
336
+ @io = Redisk::IO.new('test.log')
337
+ @io.truncate
338
+ @logger = Logger.new(@io)
339
+ end
340
+
341
+ it 'should write to the log' do
342
+ @logger.info "This should be info"
343
+ @logger.warn "This should be warn"
344
+ @io.length.should == 2
345
+ end
346
+
347
+ end
348
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Redis::Logger do
4
+ before(:each) do
5
+ @io = Redisk::IO.new('test.log').truncate(0)
6
+ end
7
+
8
+ describe 'initializing' do
9
+ before do
10
+ @logger = Redisk::Logger.new('test.log')
11
+ end
12
+
13
+ it 'should initialize a logger' do
14
+ @logger.logger.should be_instance_of(::Logger)
15
+ end
16
+
17
+ it 'should initialize an io' do
18
+ @logger.io.should be_instance_of(Redisk::IO)
19
+ @logger.io.name.should == 'test.log'
20
+ end
21
+
22
+ it 'should return a Redis::Logger' do
23
+ @logger.should be_instance_of(Redisk::Logger)
24
+ end
25
+ end
26
+
27
+ describe 'logging' do
28
+
29
+ it 'should delegate logging methods to logger' do
30
+ @logger = Redisk::Logger.new('test.log')
31
+ @logger.info('info').should == true
32
+ @logger.length.should == 1
33
+ end
34
+
35
+ end
36
+
37
+ describe 'initializing with :truncate' do
38
+
39
+ it 'should trim the log after _n lines' do
40
+ @logger = Redisk::Logger.new('test.log', :truncate => 5)
41
+ 5.times do |n|
42
+ @logger.info("#{n} time")
43
+ end
44
+ @logger.length.should == 5
45
+ @logger.info("another time")
46
+ @logger.length.should == 5
47
+ @logger.info("and again")
48
+ @logger.length.should == 5
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Redisk::Stat do
4
+ before(:each) do
5
+ @stat = Redisk::Stat.new('test_stat')
6
+ end
7
+
8
+ after(:each) do
9
+ keys = Redisk.redis.keys('test_stat:*')
10
+ keys.each do |key|
11
+ Redisk.redis.del key.gsub(/^redisk\:/, '')
12
+ end
13
+ end
14
+
15
+ describe 'initialize' do
16
+
17
+ it 'should return a Redisk::Stat object' do
18
+ @stat.should be_instance_of(Redisk::Stat)
19
+ end
20
+
21
+ it 'should set name' do
22
+ @stat.name.should == 'test_stat'
23
+ end
24
+
25
+ it 'should write defaults for new files' do
26
+ @stat.size.should == 0
27
+ @stat.atime.should be_instance_of(Time)
28
+ end
29
+ end
30
+
31
+ describe 'atime' do
32
+
33
+ it 'should return a time' do
34
+ @stat.atime.should be_instance_of(Time)
35
+ end
36
+
37
+ it 'should return the last access time' do
38
+ inital_time = @stat.atime
39
+ sleep 1
40
+ @stat = Redisk::Stat.new('test_stat')
41
+ new_time = @stat.atime
42
+ new_time.should > inital_time
43
+ end
44
+
45
+ end
46
+
47
+ describe 'ctime' do
48
+
49
+ it 'should return a time' do
50
+ @stat.ctime.should be_instance_of(Time)
51
+ end
52
+
53
+ it 'should return the last changed time' do
54
+ inital_time = @stat.ctime
55
+ sleep 1
56
+ @stat.write_attribute('size', 4000)
57
+ new_time = @stat.ctime
58
+ new_time.should > inital_time
59
+ end
60
+
61
+ end
62
+
63
+ describe 'mtime' do
64
+
65
+ it 'should return a time' do
66
+ @stat.mtime.should be_instance_of(Time)
67
+ end
68
+
69
+ it 'should return the last modification of the io' do
70
+ inital_time = @stat.mtime
71
+ sleep 1
72
+ @io = Redisk::IO.new('test_stat')
73
+ @io.write '123'
74
+ new_time = @stat.mtime
75
+ new_time.should > inital_time
76
+ end
77
+ end
78
+
79
+ describe 'size' do
80
+ it 'should return the length of the IO in bytes' do
81
+ @io = Redisk::IO.new('test_stat')
82
+ @io.write '123'
83
+ @stat.size.should == 3
84
+ end
85
+ end
86
+
87
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,47 @@
1
+ dir = File.dirname(File.expand_path(__FILE__))
2
+ $LOAD_PATH.unshift(dir)
3
+ $LOAD_PATH.unshift(File.join(dir, '..', 'lib'))
4
+ require 'fileutils'
5
+ require 'rubygems'
6
+ require 'redisk'
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ end
13
+
14
+ ## Redis test bootsrapping
15
+ ## Stolen with love from @defunkt's resque
16
+ ## github.com/defunkt/resque
17
+
18
+ #
19
+ # make sure we can run redis
20
+ #
21
+
22
+ if !system("which redis-server")
23
+ puts '', "** can't find `redis-server` in your path"
24
+ puts "** try running `sudo rake install`"
25
+ abort ''
26
+ end
27
+
28
+
29
+ #
30
+ # start our own redis when the tests start,
31
+ # kill it when they end
32
+ #
33
+
34
+ at_exit do
35
+ exit_with = $!
36
+ next unless exit_with.is_a?(SystemExit)
37
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
38
+ puts "Killing test redis server..."
39
+ `rm -f #{dir}/dump.rdb`
40
+ Process.kill("KILL", pid.to_i)
41
+ exit exit_with.status
42
+ end
43
+
44
+ FileUtils.mkdir_p(File.join(dir, 'tmp'))
45
+ puts "Starting redis for testing at localhost:9736..."
46
+ puts `redis-server #{dir}/redis-test.conf`
47
+ Redisk.redis = 'localhost:9736'