2015-11-13 23:44:26 +08:00
|
|
|
-- Get the connection to redis from cache or create a new one
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._connect_to_redis(user_id name)
|
|
|
|
RETURNS boolean AS $$
|
|
|
|
if user_id in GD and 'redis_connection' in GD[user_id]:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
from cartodb_geocoder import redis_helper
|
2015-11-16 21:01:51 +08:00
|
|
|
config_params = plpy.execute("select c.host, c.port, c.timeout, c.db from cdb_geocoder_server._get_redis_conf() c;")[0]
|
|
|
|
redis_connection = redis_helper.RedisHelper(config_params['host'], config_params['port'], config_params['db']).redis_connection()
|
2015-11-13 23:44:26 +08:00
|
|
|
GD[user_id] = {'redis_connection': redis_connection}
|
|
|
|
return True
|
2015-11-16 21:01:51 +08:00
|
|
|
$$ LANGUAGE plpythonu;
|
|
|
|
|
|
|
|
CREATE TYPE cdb_geocoder_server._redis_conf_params AS (
|
|
|
|
host text,
|
|
|
|
port int,
|
|
|
|
timeout float,
|
|
|
|
db text
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_redis_conf()
|
|
|
|
RETURNS cdb_geocoder_server._redis_conf_params AS $$
|
|
|
|
conf = plpy.execute("SELECT cdb_geocoder_server._config_get('redis_conf') conf")[0]['conf']
|
|
|
|
if conf is None:
|
|
|
|
plpy.error("There is no redis configuration defined")
|
|
|
|
else:
|
|
|
|
import json
|
|
|
|
params = json.loads(conf)
|
|
|
|
return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'db': params['db'] }
|
2015-11-13 23:44:26 +08:00
|
|
|
$$ LANGUAGE plpythonu;
|