Update redisql/README.md
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
```python
|
```python
|
||||||
#
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
|
|
||||||
class SQLiteRedis:
|
class SQLiteRedis:
|
||||||
def __init__(self, db_name=':memory:'):
|
def __init__(self, db_name='redis.sqlite'):
|
||||||
self.conn = sqlite3.connect(db_name)
|
self.conn = sqlite3.connect(db_name, isolation_level=None)
|
||||||
|
self.conn.execute('PRAGMA journal_mode=WAL')
|
||||||
self.cur = self.conn.cursor()
|
self.cur = self.conn.cursor()
|
||||||
self._create_tables()
|
self._create_tables()
|
||||||
|
|
||||||
@@ -14,21 +14,17 @@ class SQLiteRedis:
|
|||||||
(key TEXT PRIMARY KEY, value TEXT)''')
|
(key TEXT PRIMARY KEY, value TEXT)''')
|
||||||
self.cur.execute('''CREATE TABLE IF NOT EXISTS expiry
|
self.cur.execute('''CREATE TABLE IF NOT EXISTS expiry
|
||||||
(key TEXT PRIMARY KEY, expire_at INTEGER)''')
|
(key TEXT PRIMARY KEY, expire_at INTEGER)''')
|
||||||
self.conn.commit()
|
|
||||||
self.cur.execute('''CREATE TABLE IF NOT EXISTS list_store
|
self.cur.execute('''CREATE TABLE IF NOT EXISTS list_store
|
||||||
(key TEXT, idx INTEGER, value TEXT,
|
(key TEXT, idx INTEGER, value TEXT,
|
||||||
PRIMARY KEY (key, idx))''')
|
PRIMARY KEY (key, idx))''')
|
||||||
self.conn.commit()
|
|
||||||
self.cur.execute('''CREATE TABLE IF NOT EXISTS hash_store
|
self.cur.execute('''CREATE TABLE IF NOT EXISTS hash_store
|
||||||
(key TEXT, field TEXT, value TEXT,
|
(key TEXT, field TEXT, value TEXT,
|
||||||
PRIMARY KEY (key, field))''')
|
PRIMARY KEY (key, field))''')
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def set(self, key, value):
|
def set(self, key, value):
|
||||||
self.cur.execute("REPLACE INTO kv_store (key, value) VALUES (?, ?)",
|
self.cur.execute("REPLACE INTO kv_store (key, value) VALUES (?, ?)",
|
||||||
(key, value))
|
(key, value))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
self.cur.execute("SELECT value FROM kv_store WHERE key = ?", (key,))
|
self.cur.execute("SELECT value FROM kv_store WHERE key = ?", (key,))
|
||||||
@@ -40,13 +36,11 @@ class SQLiteRedis:
|
|||||||
def delete(self, key):
|
def delete(self, key):
|
||||||
self.cur.execute("DELETE FROM kv_store WHERE key = ?", (key,))
|
self.cur.execute("DELETE FROM kv_store WHERE key = ?", (key,))
|
||||||
self.cur.execute("DELETE FROM expiry WHERE key = ?", (key,))
|
self.cur.execute("DELETE FROM expiry WHERE key = ?", (key,))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def expire(self, key, seconds):
|
def expire(self, key, seconds):
|
||||||
expire_at = int(time.time()) + seconds
|
expire_at = int(time.time()) + seconds
|
||||||
self.cur.execute("REPLACE INTO expiry (key, expire_at) VALUES (?, ?)",
|
self.cur.execute("REPLACE INTO expiry (key, expire_at) VALUES (?, ?)",
|
||||||
(key, expire_at))
|
(key, expire_at))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def ttl(self, key):
|
def ttl(self, key):
|
||||||
self.cur.execute("SELECT expire_at FROM expiry WHERE key = ?", (key,))
|
self.cur.execute("SELECT expire_at FROM expiry WHERE key = ?", (key,))
|
||||||
@@ -61,7 +55,6 @@ class SQLiteRedis:
|
|||||||
self.cur.execute("DELETE FROM kv_store WHERE key IN "
|
self.cur.execute("DELETE FROM kv_store WHERE key IN "
|
||||||
"(SELECT key FROM expiry WHERE expire_at <= ?)", (now,))
|
"(SELECT key FROM expiry WHERE expire_at <= ?)", (now,))
|
||||||
self.cur.execute("DELETE FROM expiry WHERE expire_at <= ?", (now,))
|
self.cur.execute("DELETE FROM expiry WHERE expire_at <= ?", (now,))
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
@@ -71,7 +64,6 @@ class SQLiteRedis:
|
|||||||
self.cur.execute("INSERT INTO list_store (key, idx, value) "
|
self.cur.execute("INSERT INTO list_store (key, idx, value) "
|
||||||
"SELECT ?, COALESCE(MIN(idx), 0) - 1, ? "
|
"SELECT ?, COALESCE(MIN(idx), 0) - 1, ? "
|
||||||
"FROM list_store WHERE key = ?", (key, value, key))
|
"FROM list_store WHERE key = ?", (key, value, key))
|
||||||
self.conn.commit()
|
|
||||||
return self.llen(key)
|
return self.llen(key)
|
||||||
|
|
||||||
def rpush(self, key, *values):
|
def rpush(self, key, *values):
|
||||||
@@ -79,7 +71,6 @@ class SQLiteRedis:
|
|||||||
self.cur.execute("INSERT INTO list_store (key, idx, value) "
|
self.cur.execute("INSERT INTO list_store (key, idx, value) "
|
||||||
"SELECT ?, COALESCE(MAX(idx), -1) + 1, ? "
|
"SELECT ?, COALESCE(MAX(idx), -1) + 1, ? "
|
||||||
"FROM list_store WHERE key = ?", (key, value, key))
|
"FROM list_store WHERE key = ?", (key, value, key))
|
||||||
self.conn.commit()
|
|
||||||
return self.llen(key)
|
return self.llen(key)
|
||||||
|
|
||||||
def lpop(self, key):
|
def lpop(self, key):
|
||||||
@@ -90,7 +81,6 @@ class SQLiteRedis:
|
|||||||
self.cur.execute("DELETE FROM list_store WHERE key = ? "
|
self.cur.execute("DELETE FROM list_store WHERE key = ? "
|
||||||
"AND idx = (SELECT MIN(idx) FROM list_store WHERE key = ?)",
|
"AND idx = (SELECT MIN(idx) FROM list_store WHERE key = ?)",
|
||||||
(key, key))
|
(key, key))
|
||||||
self.conn.commit()
|
|
||||||
return result[0]
|
return result[0]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -107,7 +97,6 @@ class SQLiteRedis:
|
|||||||
def hset(self, key, field, value):
|
def hset(self, key, field, value):
|
||||||
self.cur.execute("REPLACE INTO hash_store (key, field, value) VALUES (?, ?, ?)",
|
self.cur.execute("REPLACE INTO hash_store (key, field, value) VALUES (?, ?, ?)",
|
||||||
(key, field, value))
|
(key, field, value))
|
||||||
self.conn.commit()
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def hget(self, key, field):
|
def hget(self, key, field):
|
||||||
@@ -122,7 +111,6 @@ class SQLiteRedis:
|
|||||||
self.cur.execute("DELETE FROM hash_store WHERE key = ? AND field = ?",
|
self.cur.execute("DELETE FROM hash_store WHERE key = ? AND field = ?",
|
||||||
(key, field))
|
(key, field))
|
||||||
deleted += self.cur.rowcount
|
deleted += self.cur.rowcount
|
||||||
self.conn.commit()
|
|
||||||
return deleted
|
return deleted
|
||||||
|
|
||||||
def hlen(self, key):
|
def hlen(self, key):
|
||||||
@@ -152,6 +140,4 @@ r.hset("myhash", "name", "John")
|
|||||||
r.hset("myhash", "age", "30")
|
r.hset("myhash", "age", "30")
|
||||||
print(r.hget("myhash", "name")) # 'John'
|
print(r.hget("myhash", "name")) # 'John'
|
||||||
print(r.hgetall("myhash")) # {'name': 'John', 'age': '30'}
|
print(r.hgetall("myhash")) # {'name': 'John', 'age': '30'}
|
||||||
#
|
|
||||||
|
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user