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