Skip to content

Commit 338be6a

Browse files
committed
Added memcache.conf option
Change-Id: Idfcb97421a5c1476b776acef94c7b997dab55aa9
1 parent 76afb46 commit 338be6a

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

etc/memcache.conf-sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[memcache]
2+
# You can use this single conf file instead of having memcache_servers set in
3+
# several other conf files under [filter:cache] for example. You can specify
4+
# multiple servers separated with commas, as in: 10.1.2.3:11211,10.1.2.4:11211
5+
# memcache_servers = 127.0.0.1:11211

etc/proxy-server.conf-sample

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ use = egg:swift#memcache
104104
# set log_facility = LOG_LOCAL0
105105
# set log_level = INFO
106106
# set log_headers = False
107-
# Default for memcache_servers is below, but you can specify multiple servers
108-
# with the format: 10.1.2.3:11211,10.1.2.4:11211
107+
# Default for memcache_servers is to try to read the property from
108+
# memcache.conf (see memcache.conf-sample) or lacking that file, it will
109+
# default to the value below. You can specify multiple servers separated with
110+
# commas, as in: 10.1.2.3:11211,10.1.2.4:11211
109111
# memcache_servers = 127.0.0.1:11211
110112

111113
[filter:ratelimit]

swift/common/middleware/memcache.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import os
17+
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
18+
1619
from swift.common.memcached import MemcacheRing
1720

1821

@@ -23,9 +26,21 @@ class MemcacheMiddleware(object):
2326

2427
def __init__(self, app, conf):
2528
self.app = app
26-
self.memcache = MemcacheRing([s.strip() for s in
27-
conf.get('memcache_servers', '127.0.0.1:11211').split(',')
28-
if s.strip()])
29+
self.memcache_servers = conf.get('memcache_servers')
30+
if not self.memcache_servers:
31+
path = os.path.join(conf.get('swift_dir', '/etc/swift'),
32+
'memcache.conf')
33+
memcache_conf = ConfigParser()
34+
if memcache_conf.read(path):
35+
try:
36+
self.memcache_servers = \
37+
memcache_conf.get('memcache', 'memcache_servers')
38+
except (NoSectionError, NoOptionError):
39+
pass
40+
if not self.memcache_servers:
41+
self.memcache_servers = '127.0.0.1:11211'
42+
self.memcache = MemcacheRing(
43+
[s.strip() for s in self.memcache_servers.split(',') if s.strip()])
2944

3045
def __call__(self, env, start_response):
3146
env['swift.cache'] = self.memcache

test/unit/common/middleware/test_memcache.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
import unittest
17+
from ConfigParser import NoSectionError, NoOptionError
1718

1819
from webob import Request
1920

@@ -24,6 +25,34 @@ class FakeApp(object):
2425
def __call__(self, env, start_response):
2526
return env
2627

28+
29+
class ExcConfigParser(object):
30+
31+
def read(self, path):
32+
raise Exception('read called with %r' % path)
33+
34+
35+
class EmptyConfigParser(object):
36+
37+
def read(self, path):
38+
return False
39+
40+
41+
class SetConfigParser(object):
42+
43+
def read(self, path):
44+
return True
45+
46+
def get(self, section, option):
47+
if section == 'memcache':
48+
if option == 'memcache_servers':
49+
return '1.2.3.4:5'
50+
else:
51+
raise NoOptionError(option)
52+
else:
53+
raise NoSectionError(option)
54+
55+
2756
def start_response(*args):
2857
pass
2958

@@ -38,5 +67,60 @@ def test_cache_middleware(self):
3867
self.assertTrue('swift.cache' in resp)
3968
self.assertTrue(isinstance(resp['swift.cache'], MemcacheRing))
4069

70+
def test_conf_default_read(self):
71+
orig_parser = memcache.ConfigParser
72+
memcache.ConfigParser = ExcConfigParser
73+
exc = None
74+
try:
75+
app = memcache.MemcacheMiddleware(FakeApp(), {})
76+
except Exception, err:
77+
exc = err
78+
finally:
79+
memcache.ConfigParser = orig_parser
80+
self.assertEquals(str(exc),
81+
"read called with '/etc/swift/memcache.conf'")
82+
83+
def test_conf_set_no_read(self):
84+
orig_parser = memcache.ConfigParser
85+
memcache.ConfigParser = ExcConfigParser
86+
exc = None
87+
try:
88+
app = memcache.MemcacheMiddleware(
89+
FakeApp(), {'memcache_servers': '1.2.3.4:5'})
90+
except Exception, err:
91+
exc = err
92+
finally:
93+
memcache.ConfigParser = orig_parser
94+
self.assertEquals(exc, None)
95+
96+
def test_conf_default(self):
97+
orig_parser = memcache.ConfigParser
98+
memcache.ConfigParser = EmptyConfigParser
99+
try:
100+
app = memcache.MemcacheMiddleware(FakeApp(), {})
101+
finally:
102+
memcache.ConfigParser = orig_parser
103+
self.assertEquals(app.memcache_servers, '127.0.0.1:11211')
104+
105+
def test_conf_from_extra_conf(self):
106+
orig_parser = memcache.ConfigParser
107+
memcache.ConfigParser = SetConfigParser
108+
try:
109+
app = memcache.MemcacheMiddleware(FakeApp(), {})
110+
finally:
111+
memcache.ConfigParser = orig_parser
112+
self.assertEquals(app.memcache_servers, '1.2.3.4:5')
113+
114+
def test_conf_from_inline_conf(self):
115+
orig_parser = memcache.ConfigParser
116+
memcache.ConfigParser = SetConfigParser
117+
try:
118+
app = memcache.MemcacheMiddleware(
119+
FakeApp(), {'memcache_servers': '6.7.8.9:10'})
120+
finally:
121+
memcache.ConfigParser = orig_parser
122+
self.assertEquals(app.memcache_servers, '6.7.8.9:10')
123+
124+
41125
if __name__ == '__main__':
42126
unittest.main()

0 commit comments

Comments
 (0)