|
| 1 | +local redis = require("redis") |
| 2 | +local M = {} |
| 3 | + |
| 4 | +function M.create_client() |
| 5 | + local host = "127.0.0.1" |
| 6 | + local port = 6379 |
| 7 | + local client, err = redis.connect(host, port) |
| 8 | + if not client then |
| 9 | + error("Failed to connect to Redis: " .. tostring(err)) |
| 10 | + end |
| 11 | + return client |
| 12 | +end |
| 13 | + |
| 14 | +-- String |
| 15 | +function M.set_string(client, key, value) |
| 16 | + client:set(key, value) |
| 17 | +end |
| 18 | + |
| 19 | +function M.get_string(client, key) |
| 20 | + return client:get(key) |
| 21 | +end |
| 22 | + |
| 23 | +-- Hash |
| 24 | +function M.set_hash(client, key, field, value) |
| 25 | + client:hset(key, field, value) |
| 26 | +end |
| 27 | + |
| 28 | +function M.get_hash(client, key, field) |
| 29 | + return client:hget(key, field) |
| 30 | +end |
| 31 | + |
| 32 | +function M.get_all_hash(client, key) |
| 33 | + return client:hgetall(key) |
| 34 | +end |
| 35 | + |
| 36 | +-- List |
| 37 | +function M.push_list(client, key, value) |
| 38 | + client:rpush(key, value) |
| 39 | +end |
| 40 | + |
| 41 | +function M.push_list_left(client, key, value) |
| 42 | + client:lpush(key, value) |
| 43 | +end |
| 44 | + |
| 45 | +function M.get_list(client, key) |
| 46 | + return client:lrange(key, 0, -1) |
| 47 | +end |
| 48 | + |
| 49 | +function M.pop_list_left(client, key) |
| 50 | + return client:lpop(key) |
| 51 | +end |
| 52 | + |
| 53 | +function M.pop_list_right(client, key) |
| 54 | + return client:rpop(key) |
| 55 | +end |
| 56 | + |
| 57 | +-- Set |
| 58 | +function M.add_set(client, key, value) |
| 59 | + client:sadd(key, value) |
| 60 | +end |
| 61 | + |
| 62 | +function M.get_set(client, key) |
| 63 | + return client:smembers(key) |
| 64 | +end |
| 65 | + |
| 66 | +function M.is_member_of_set(client, key, value) |
| 67 | + return client:sismember(key, value) |
| 68 | +end |
| 69 | + |
| 70 | +function M.remove_from_set(client, key, value) |
| 71 | + return client:srem(key, value) |
| 72 | +end |
| 73 | + |
| 74 | +-- Sorted Set |
| 75 | +function M.add_sorted_set(client, key, score, value) |
| 76 | + client:zadd(key, score, value) |
| 77 | +end |
| 78 | + |
| 79 | +function M.get_sorted_set(client, key) |
| 80 | + return client:zrange(key, 0, -1, "WITHSCORES") |
| 81 | +end |
| 82 | + |
| 83 | +function M.get_sorted_set_within_range(client, key, min_score, max_score) |
| 84 | + return client:zrangebyscore(key, min_score, max_score, "WITHSCORES") |
| 85 | +end |
| 86 | + |
| 87 | +-- Bitmap |
| 88 | +function M.set_bit(client, key, offset, value) |
| 89 | + client:setbit(key, offset, value) |
| 90 | +end |
| 91 | + |
| 92 | +function M.get_bit(client, key, offset) |
| 93 | + return client:getbit(key, offset) |
| 94 | +end |
| 95 | + |
| 96 | +function M.count_bits(client, key, start_index, end_index) |
| 97 | + return client:bitcount(key, start_index, end_index) |
| 98 | +end |
| 99 | + |
| 100 | +function M.bitfield_operations(client, key, ...) |
| 101 | + return client:bitfield(key, ...) |
| 102 | +end |
| 103 | + |
| 104 | +return M |
0 commit comments