summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Haritonov <reclosedev@gmail.com>2015-04-28 22:33:21 +0400
committerRoman Haritonov <reclosedev@gmail.com>2015-04-28 22:33:21 +0400
commit41f172ca2be3eb47e2df55ac7b1f1ec63566c16e (patch)
tree246eb323dd08e6e5143d54716e9bf92617bc5a87
parentb997cb3e2550958d7b3a561e31b3328ecc407493 (diff)
parent0cbec7350dc229bcfe97112453430b8a6e8234f5 (diff)
downloadrequests-cache-41f172ca2be3eb47e2df55ac7b1f1ec63566c16e.tar.gz
Merge pull request #50 from HappierApp/master
Better transactional handling in sqlite
-rw-r--r--requests_cache/backends/storage/dbdict.py22
-rw-r--r--setup.py2
2 files changed, 8 insertions, 16 deletions
diff --git a/requests_cache/backends/storage/dbdict.py b/requests_cache/backends/storage/dbdict.py
index ef0f516..0d6ce59 100644
--- a/requests_cache/backends/storage/dbdict.py
+++ b/requests_cache/backends/storage/dbdict.py
@@ -10,7 +10,7 @@ from collections import MutableMapping
import sqlite3 as sqlite
from contextlib import contextmanager
try:
- import threading
+ import threading
except ImportError:
import dummy_threading as threading
try:
@@ -21,7 +21,6 @@ except ImportError:
from requests_cache.compat import bytes
-
class DbDict(MutableMapping):
""" DbDict - a dictionary-like object for saving large datasets to `sqlite` database
@@ -122,21 +121,14 @@ class DbDict(MutableMapping):
def __setitem__(self, key, item):
with self.connection(True) as con:
- if con.execute("select key from `%s` where key=?" %
- self.table_name, (key,)).fetchone():
- con.execute("update `%s` set value=? where key=?" %
- self.table_name, (item, key))
- else:
- con.execute("insert into `%s` (key,value) values (?,?)" %
- self.table_name, (key, item))
+ con.execute("insert or replace into `%s` (key,value) values (?,?)" %
+ self.table_name, (key, item))
def __delitem__(self, key):
with self.connection(True) as con:
- if con.execute("select key from `%s` where key=?" %
- self.table_name, (key,)).fetchone():
- con.execute("delete from `%s` where key=?" %
- self.table_name, (key,))
- else:
+ cur = con.execute("delete from `%s` where key=?" %
+ self.table_name, (key,))
+ if not cur.rowcount:
raise KeyError
def __iter__(self):
@@ -148,7 +140,7 @@ class DbDict(MutableMapping):
def __len__(self):
with self.connection() as con:
return con.execute("select count(key) from `%s`" %
- self.table_name).fetchone()[0]
+ self.table_name).fetchone()[0]
def clear(self):
with self.connection(True) as con:
diff --git a/setup.py b/setup.py
index 8a57e71..78801dd 100644
--- a/setup.py
+++ b/setup.py
@@ -20,7 +20,7 @@ setup(
packages=['requests_cache',
'requests_cache.backends',
'requests_cache.backends.storage'],
- version='0.4.9',
+ version='0.4.10',
description='Persistent cache for requests library',
author='Roman Haritonov',
author_email='reclosedev@gmail.com',