Skip to content

Commit

Permalink
Downgrade SQLite upserts to 'update if exists' + add 3.9-dev to travi…
Browse files Browse the repository at this point in the history
…s vers

Unfortunately, SQLite upserts (`ON CONFLICT(name) DO UPDATE xxx`) are a rather new feature (released as of SQLite 3.24.0 2018-06-04),
and it appears that the latest SQLite3 version in Ubuntu 18.04 (Bionic)'s repos is 3.22.0 - meaning that upserts are likely to be unusable
on many systems.

Due to this issue, upsert statements in `SqliteCacheManager` and `AsyncSqliteCacheManager` have been replaced with standard INSERT statements,
and the `update_cache_key` method (in both sync and async classes) now uses a standard `UPDATE` statement, which should work on all systems.

Additionally, `3.9-dev` has now been added to `.travis.yml`, but marked with `allow_failures` to avoid bugs in 3.9 (or problems with third-party libraries
that haven't yet been patched to work with 3.9) causing the travis build to fail.
  • Loading branch information
Someguy123 committed Oct 7, 2020
1 parent 3cb4ca1 commit a91b015
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ python:
- "3.6"
- "3.7"
- "3.8"
- "3.9-dev"
- "nightly"
jobs:
allow_failures:
# Sometimes nightly builds are broken, or contain breaking changes that affect dependencies we have
# no control over. Thus we don't want a failed nightly run to cause Travis to consider the build broken.
- python: 'nightly'
- python: '3.9-dev'
before_install:
- sudo apt remove -y sqlite3
- sudo apt-add-repository -y ppa:travis-ci/sqlite3
- sudo apt-get update -qy
- sudo apt-get -y install sqlite3
- sudo apt-get install -qy sqlite3
- sudo apt-get install -qy iputils-ping
- "sudo rsync -avh 'rsync://files.privex.io/cdn/extras/GeoIP/*.mmdb' /usr/share/GeoIP/"
install:
Expand Down
40 changes: 24 additions & 16 deletions privex/helpers/cache/post_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,22 @@ def cache_key_exists(self, name: str) -> bool:

def insert_cache_key(self, name: str, value: Any, expires_secs: Number = None, expires_at: Union[Number, datetime] = None):
expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
log.debug("Inserting/updating cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
log.debug("Inserting cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
# return self.action(
# "INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?) "
# "ON CONFLICT(name) DO UPDATE SET value=?,expires_at=?;",
# (name, value, expires_at, value, expires_at)
# )
return self.action(
"INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?) "
"ON CONFLICT(name) DO "
"UPDATE SET value=?,expires_at=?;",
(name, value, expires_at, value, expires_at)
"INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?);",
(name, value, expires_at)
)

def update_cache_key(self, name: str, value: Any, expires_secs: Number = None, expires_at: Union[Number, datetime] = None):
# expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
log.debug("Updating cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
return self.insert_cache_key(name, value, expires_at=expires_at, expires_secs=expires_secs)
# return self.action("UPDATE pvcache SET value = ?, expires_at = ? WHERE name = ?;", (value, name, expires_at))
# return self.insert_cache_key(name, value, expires_at=expires_at, expires_secs=expires_secs)
return self.action("UPDATE pvcache SET value = ?, expires_at = ? WHERE name = ?;", (value, expires_at, name))

def set_cache_key(self, name: str, value: Any, expires_secs: Number = None, expires_at: Union[Number, datetime] = None):
# expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
Expand Down Expand Up @@ -225,19 +228,24 @@ async def cache_key_exists(self, name: str) -> bool:

async def insert_cache_key(self, name: str, value: Any, expires_secs: Number = None, expires_at: Union[Number, datetime] = None):
expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
log.debug("Inserting/updating cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
log.debug("Inserting cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
# return await await_if_needed(self.action(
# "INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?) "
# "ON CONFLICT(name) DO UPDATE SET value=?,expires_at=?;",
# (name, value, expires_at, value, expires_at)
# ))
return await await_if_needed(self.action(
"INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?) "
"ON CONFLICT(name) DO "
"UPDATE SET value=?,expires_at=?;",
(name, value, expires_at, value, expires_at)
"INSERT INTO pvcache (name, value, expires_at) VALUES (?, ?, ?);",
(name, value, expires_at)
))

async def update_cache_key(self, name: str, value: Any, expires_secs: Number = None, expires_at: Union[Number, datetime] = None):
# expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
log.debug("Updating cache key '%s' with expires_at = '%s' and value: %s", name, expires_at, value)
return await self.insert_cache_key(name, value, expires_at=expires_at, expires_secs=expires_secs)
# return self.action("UPDATE pvcache SET value = ?, expires_at = ? WHERE name = ?;", (value, name, expires_at))
# return await self.insert_cache_key(name, value, expires_at=expires_at, expires_secs=expires_secs)
return await await_if_needed(
self.action("UPDATE pvcache SET value = ?, expires_at = ? WHERE name = ?;", (value, expires_at, name))
)

async def set_cache_key(self, name: str, value: T, expires_secs: Number = None, expires_at: Union[Number, datetime] = None) -> T:
expires_at = self._calc_expires(expires_at=expires_at, expires_secs=expires_secs)
Expand Down

0 comments on commit a91b015

Please sign in to comment.