/
cachetastic_drb_server
115 lines (93 loc) · 2.81 KB
/
cachetastic_drb_server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
#!/usr/local/bin/ruby
require 'thread'
require 'drb'
require 'singleton'
require 'optparse'
require 'optparse/time'
require 'ostruct'
# require the necessary gems:
require 'rubygems'
gem 'mack-facets'
require 'mack-facets'
gem 'configatron'
require 'configatron'
gem 'cachetastic'
require 'cachetastic'
$SAFE = 1
SERVER_OPTIONS = OpenStruct.new
SERVER_OPTIONS.host = "127.0.0.1"
SERVER_OPTIONS.port = "61676"
SERVER_OPTIONS.config_file = nil
SERVER_OPTIONS.verbose = false
SERVER_OPTIONS.really_verbose = false
opts = OptionParser.new do |opts|
opts.on("-h [HOST]") do |v|
SERVER_OPTIONS.host = v
end
opts.on("-p [PORT]", Integer) do |v|
SERVER_OPTIONS.port = v
end
opts.on("-c [CONFIG_FILE]") do |v|
SERVER_OPTIONS.config_file = v
end
opts.on("-v") do |v|
SERVER_OPTIONS.verbose = true
end
opts.on("-rv") do |v|
SERVER_OPTIONS.verbose = true
SERVER_OPTIONS.really_verbose = true
end
end
opts.parse!(ARGV)
if SERVER_OPTIONS.config_file
configatron.configure_from_yaml(SERVER_OPTIONS.config_file)
end
module Cachetastic
module Drb
class Server
include Singleton
def initialize
DRb.start_service("druby://#{SERVER_OPTIONS.host}:#{SERVER_OPTIONS.port}", self)
puts "Cachetastic::Drb::Server listening for connection..."
DRb.thread.join
end # initialize
def get(ns, key)
puts "get: #{ns}.#{key}" if SERVER_OPTIONS.verbose
puts cache_for_namespace(ns).inspect if SERVER_OPTIONS.really_verbose
value = cache_for_namespace(ns).get(key)
puts "\tvalue.class: #{value.class}" if SERVER_OPTIONS.verbose
value
end
def set(ns, key, value, expiry = 0)
puts "set: #{ns}.#{key}: #{value}" if SERVER_OPTIONS.verbose
cache_for_namespace(ns).set(key, value)
puts cache_for_namespace(ns).inspect if SERVER_OPTIONS.really_verbose
value
end
def delete(ns, key, delay = 0)
puts "delete: #{ns}.#{key}" if SERVER_OPTIONS.verbose
cache_for_namespace(ns).delete(key, delay = 0)
puts cache_for_namespace(ns).inspect if SERVER_OPTIONS.really_verbose
end
def expire_all(ns)
puts "expire_all: #{ns}" if SERVER_OPTIONS.verbose
instance_variable_set("@store_for_#{ns}", Cachetastic::Adapters::LocalMemory.new("store_for_#{ns}"))
end
def ping
true
end
private
def cache_for_namespace(ns)
# puts "@store_for_#{ns}"
c = instance_variable_get("@store_for_#{ns}")
if c.nil?
instance_variable_set("@store_for_#{ns}", Cachetastic::Adapters::LocalMemory.new("store_for_#{ns}"))
c = instance_variable_get("@store_for_#{ns}")
end
c
end
end # Server
end # Drb
end # Cachetastic
# let's start `er up!
Cachetastic::Drb::Server.instance