Skip to content

Commit da854f8

Browse files
Overhaul... try this for memcached
1 parent f078193 commit da854f8

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Flask==0.10.1
2-
Flask-Cache==0.13.1
32
Flask-SSLify==0.1.4
43
Jinja2==2.7.3
54
MarkupSafe==0.23
@@ -13,6 +12,7 @@ numpy==1.8.1
1312
openpyxl==1.8.6
1413
pandas==0.14.0
1514
pylibmc==1.3.0
15+
python-binary-memcached==0.24.2
1616
python-dateutil==2.2
1717
pytz==2014.4
1818
redis==2.10.1

server.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import redis
88
import pandas as pd
9+
import bmemcached
910

1011
from flask import Flask
1112
from flask import request
@@ -39,16 +40,13 @@
3940
sslify = SSLify(app)
4041

4142
if app.config['DEBUG']:
42-
cache = Cache(app,config={'CACHE_TYPE': 'null'})
43+
cache = bmemcached.Client(servers=['127.0.0.1:11211'])
4344
else:
44-
cache = Cache(app, config={
45-
'CACHE_TYPE': 'memcached',
46-
'CACHE_MEMCACHED_SERVERS': [ os.environ['MEMCACHEDCLOUD_SERVERS'] ],
47-
'CACHE_MEMCACHED_USERNAME': os.environ['MEMCACHEDCLOUD_USERNAME'],
48-
'CACHE_MEMCACHED_PASSWORD': os.environ['MEMCACHEDCLOUD_PASSWORD']
49-
})
50-
app.logger.info("Cache set to memcached:")
51-
app.logger.info(cache)
45+
cache = bmemcached.Client(
46+
servers=os.environ['MEMCACHEDCLOUD_SERVERS'].split(","),
47+
username=os.environ['MEMCACHEDCLOUD_USERNAME'],
48+
password=os.environ['MEMCACHEDCLOUD_PASSWORD']
49+
)
5250

5351
redis_conn = redis.StrictRedis.from_url(os.environ['REDIS_URL'])
5452

@@ -113,13 +111,19 @@ def mean_returns(asset_ids):
113111

114112
return df.drop(asset_ids_to_eliminate)
115113

116-
@cache.memoize()
117114
def build_efficient_frontier_for(asset_ids):
118-
app.logger.info("[Cache Miss] Building efficient frontier for: %s" % asset_ids)
119-
means = mean_returns(asset_ids)
120-
covars = covariance_matrix(asset_ids)
121-
return efficient_frontier(asset_ids, means, covars)
122-
115+
cache_key = "efficient_frontier/" + "-".join(asset_ids)
116+
val = cache.get(cache_key)
117+
if val is None:
118+
app.logger.info("[Cache Miss] Building efficient frontier for: %s" % asset_ids)
119+
means = mean_returns(asset_ids)
120+
covars = covariance_matrix(asset_ids)
121+
frontier = efficient_frontier(asset_ids, means, covars)
122+
cache.set(cache_key, json.dumps(frontier))
123+
else:
124+
app.logger.info("[Cache Hit] Retreiving efficient frontier for: %s" % asset_ids)
125+
frontier = json.loads(val)
126+
return frontier
123127

124128
##########
125129
# ROUTES #
@@ -138,7 +142,7 @@ def health():
138142
@app.route('/clear_cache', methods=["GET"])
139143
def clear_cache():
140144
check_for_authorization()
141-
cache.clear()
145+
cache.flush_all()
142146
return jsonify({"success": True, "message": "Cache cleared."})
143147

144148

0 commit comments

Comments
 (0)