Skip to content

Commit

Permalink
Update the upsert command.
Browse files Browse the repository at this point in the history
Refs #1055
  • Loading branch information
Rémy HUBSCHER committed Feb 1, 2017
1 parent c04a62a commit f5e3827
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Requirements
------------

* **Python**: 2.7, 3.4+
* **Backends**: In-memory (development), Postgresql 9.4+ (production)
* **Backends**: In-memory (development), Postgresql 9.5+ (production)
10 changes: 4 additions & 6 deletions kinto/core/cache/postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,11 @@ def set(self, key, value, ttl=None):
if ttl is None:
logger.warning("No TTL for cache key %r" % key)
query = """
WITH upsert AS (
UPDATE cache SET value = :value, ttl = sec2ttl(:ttl)
WHERE key=:key
RETURNING *)
INSERT INTO cache (key, value, ttl)
SELECT :key, :value, sec2ttl(:ttl)
WHERE NOT EXISTS (SELECT * FROM upsert)
VALUES (:key, :value, sec2ttl(:ttl))
ON CONFLICT (key) DO UPDATE
SET value = :value,
ttl = sec2ttl(:ttl);
"""
value = json.dumps(value)
with self.client.connect() as conn:
Expand Down

0 comments on commit f5e3827

Please sign in to comment.