Skip to content

Commit 6bc9a2e

Browse files
authored
Merge pull request #2 from karthik2804/add_more_redis
Add redis del, publish, sadd, smembers and srem commands
2 parents 9a7dee8 + 9f67172 commit 6bc9a2e

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

py/app.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from spin_http import Request, Response, http_send
2-
from spin_redis import redis_get, redis_set
2+
from spin_redis import redis_del, redis_get, redis_incr, redis_set, redis_sadd, redis_srem, redis_smembers
33
from spin_config import config_get
44
from os import environ
55
import toml
@@ -27,6 +27,13 @@ def handle_request(request):
2727
redis_address = config_get("redis_address")
2828
redis_set(redis_address, "foo", b"bar")
2929
value = redis_get(redis_address, "foo")
30+
redis_del(redis_address, ["testIncr"])
31+
redis_incr(redis_address, "testIncr")
32+
33+
redis_sadd(redis_address, "testSets", ["hello", "world"])
34+
content = redis_smembers(redis_address, "testSets")
35+
redis_srem(redis_address, "testSets", ["hello"])
36+
3037
assert value == b"bar", f"expected \"bar\", got \"{str(value, 'utf-8')}\""
3138

3239
return Response(200,

src/lib.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ fn spin_http_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
158158
module.add_class::<HttpResponse>()
159159
}
160160

161+
#[pyo3::pyfunction]
162+
fn redis_del(address: String, keys: Vec<String>) -> PyResult<i64> {
163+
let keys = keys.iter().map(|s| s.as_str()).collect::<Vec<_>>();
164+
redis::del(&address, &keys)
165+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis set command"))))
166+
}
167+
161168
#[pyo3::pyfunction]
162169
#[pyo3(pass_module)]
163170
fn redis_get(module: &PyModule, address: String, key: String) -> PyResult<Py<PyBytes>> {
@@ -168,17 +175,56 @@ fn redis_get(module: &PyModule, address: String, key: String) -> PyResult<Py<PyB
168175
)
169176
}
170177

178+
#[pyo3::pyfunction]
179+
fn redis_incr(address: String, key: String) -> PyResult<i64> {
180+
redis::incr(&address, &key)
181+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis incr command"))))
182+
}
183+
184+
#[pyo3::pyfunction]
185+
fn redis_publish(address: String, channel: String, payload: &PyBytes) -> PyResult<()> {
186+
redis::publish(&address, &channel, payload.as_bytes())
187+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis publish command"))))
188+
}
189+
190+
#[pyo3::pyfunction]
191+
fn redis_sadd(address: String, key: String, values: Vec<String>) -> PyResult<i64> {
192+
let values = values.iter().map(|s| s.as_str()).collect::<Vec<_>>();
193+
redis::sadd(&address, &key, &values)
194+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis set command"))))
195+
}
196+
171197
#[pyo3::pyfunction]
172198
fn redis_set(address: String, key: String, value: &PyBytes) -> PyResult<()> {
173199
redis::set(&address, &key, value.as_bytes())
174200
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis set command"))))
175201
}
176202

203+
#[pyo3::pyfunction]
204+
fn redis_smembers(address: String, key: String) -> PyResult<Vec<String>> {
205+
redis::smembers(&address, &key)
206+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis set command"))))
207+
}
208+
209+
#[pyo3::pyfunction]
210+
fn redis_srem(address: String, key: String, values: Vec<String>) -> PyResult<i64> {
211+
let values = values.iter().map(|s| s.as_str()).collect::<Vec<_>>();
212+
redis::srem(&address, &key, &values)
213+
.map_err(|_| PyErr::from(Anyhow(anyhow!("Error executing Redis set command"))))
214+
}
215+
177216
#[pyo3::pymodule]
178217
#[pyo3(name = "spin_redis")]
179218
fn spin_redis_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
219+
module.add_function(pyo3::wrap_pyfunction!(redis_del, module)?)?;
180220
module.add_function(pyo3::wrap_pyfunction!(redis_get, module)?)?;
181-
module.add_function(pyo3::wrap_pyfunction!(redis_set, module)?)
221+
module.add_function(pyo3::wrap_pyfunction!(redis_incr, module)?)?;
222+
module.add_function(pyo3::wrap_pyfunction!(redis_publish, module)?)?;
223+
module.add_function(pyo3::wrap_pyfunction!(redis_sadd, module)?)?;
224+
module.add_function(pyo3::wrap_pyfunction!(redis_set, module)?)?;
225+
module.add_function(pyo3::wrap_pyfunction!(redis_smembers, module)?)?;
226+
module.add_function(pyo3::wrap_pyfunction!(redis_srem, module)?)
227+
182228
}
183229

184230
#[pyo3::pyfunction]

0 commit comments

Comments
 (0)