Skip to content

Instantly share code, notes, and snippets.

@iwillig
Created March 21, 2023 12:27
Show Gist options
  • Save iwillig/1be7c292ca562dda33077476c097f5d4 to your computer and use it in GitHub Desktop.
Save iwillig/1be7c292ca562dda33077476c097f5d4 to your computer and use it in GitHub Desktop.
(ns redis
(:import (redis.clients.jedis Jedis)
(redis.clients.jedis JedisPubSub)
(redis.clients.jedis Pipeline)))
(set! *warn-on-reflection* true)
;;;; jedis.sadd("planets", "Venus");
;; Pipeline p = jedis.pipelined();
;; p.set("fool", "bar");
;; p.zadd("foo", 1, "barowitch"); p.zadd("foo", 0, "barinsky"); p.zadd("foo", 0, "barikoviev");
;; Response<String> pipeString = p.get("fool");
;; Response<Set<String>> sose = p.zrange("foo", 0, -1);
;; p.sync();
;; int soseSize = sose.get().size();
;; Set<String> setBack = sose.get();
;; class MyListener extends JedisPubSub {
;; public void onMessage(String channel, String message) {
;; }
;; public void onSubscribe(String channel, int subscribedChannels) {
;; }
;; public void onUnsubscribe(String channel, int subscribedChannels) {
;; }
;; public void onPSubscribe(String pattern, int subscribedChannels) {
;; }
;; public void onPUnsubscribe(String pattern, int subscribedChannels) {
;; }
;; public void onPMessage(String pattern, String channel, String message) {
;; }
;; }
;; MyListener l = new MyListener();
;; jedis.subscribe(l, "foo");
(comment
(def jedis (Jedis. "http://localhost:6379"))
(.select jedis 0)
(.set jedis "test" "test")
(.get jedis "test")
(let [p (.pipelined jedis)]
(.set p "fool" "bar")
(.sync p)))
;; https://www.javadoc.io/doc/redis.clients/jedis/2.8.0/redis/clients/jedis/JedisPubSub.html#getSubscribedChannels--
(defn jedis-pub-sub
[]
(proxy [JedisPubSub] []
(onMesssage [^String channel ^String message])
(onSubscribe [^String channel ^Long subscribedChannels])
(onUnsubscribe [^String channel ^Long subscribedChannels])
(onPSubscribe [^String pattern ^Long subscribedChannels])
(onPMessage [^String pattern ^String channel ^String message])))
;; []...
(defn
^"[Ljava.lang.String;"
value-to-array-str
[values]
(into-array
(if-not (seqable? values)
[values]
values)))
(defn redis-subscribe
"subscribe(JedisPubSub jedisPubSub, java.lang.String... channels)"
[^Jedis jedis ^JedisPubSub listener channels]
(.subscribe jedis listener (value-to-array-str channels)))
;; Jedis jSubscriber = new Jedis();
;; jSubscriber.subscribe(new JedisPubSub() {
;; @Override
;; public void onMessage(String channel, String message) {
;; // handle message
;; }
;; }, "channel");
;; jPublisher.publish("channel", "test message");
;; String
;; jedis.set("events/city/rome", "32,15,223,828");
;; String cachedResponse = jedis.get("events/city/rome");
;;
;; java.lang.String set(byte[] key, byte[] value)
;; Set the string value as value of the key.
(defn redis-set
[^Jedis jedis ^String key ^String value]
(.set jedis key value))
;;
(defn redis-get
[^Jedis jedis ^String key]
(.get jedis key))
;; --------------------------------------------------
;; List
(defn redis-lpush
[^Jedis jedis ^String key values]
;; Add the string value to the head (LPUSH) or tail (RPUSH) of the list stored at key.
(.lpush jedis
key
(value-to-array-str values)))
(defn redis-rpop
;; jedis.rpop
[^Jedis jedis ^String key]
(.rpop jedis key))
;; --------------------------------------------------
;; Set
(defn redis-sadd
;; sadd(java.lang.String key, java.lang.String... members)
[^Jedis jedis ^String key value]
(.sadd jedis key (value-to-array-str value)))
(defn redis-smembers
[^Jedis jedis ^String key]
(.smembers jedis key))
(defn redis-sismember
[^Jedis jedis ^String key ^String value]
(.sismember jedis key value))
;; --------------------------------------------------
;; Hashses
;; jedis.hset("user#1", "name", "Peter");
;; jedis.hset("user#1", "job", "politician");
;; String name = jedis.hget("user#1", "name");
;; Map<String, String> fields = jedis.hgetAll("user#1");
;; String job = fields.get("job");
(defn redis-hset
[^Jedis jedis ^String key ^String field ^String value]
(.hset jedis key field value))
(defn redis-hget-all
[^Jedis jedis ^String key]
(.hgetAll jedis key))
(comment
(redis-hset
jedis
"user#1"
"name"
"Peter")
(redis-hset
jedis
"user#1"
"job"
"politican")
(redis-hget-all
jedis
"user#1")
(get (redis-hget-all
jedis
"user#1") "name")
)
;; --------------------------------------------------
;; Sorted Sets
;; Map<String, Double> scores = new HashMap<>();
;; scores.put("PlayerOne", 3000.0);
;; scores.put("PlayerTwo", 1500.0);
;; scores.put("PlayerThree", 8200.0);
;; scores.entrySet().forEach(playerScore -> {
;; jedis.zadd(key, playerScore.getValue(), playerScore.getKey());
;; });
;; String player = jedis.zrevrange("ranking", 0, 1).iterator().next();
;; long rank = jedis.zrevrank("ranking", "PlayerOne");
(defn redis-zadd
[^Jedis jedis ^String k r ^String m]
(.zadd jedis k (double r) m))
;; java.lang.String key, long start, long stop
;; zrange(java.lang.String key,
;; long start,
;; long stop)
(defn redis-zrange
[^Jedis jedis ^String key start stop]
(.zrange jedis key (long start) (long stop)))
(comment
)
(comment
;; jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike");
;;
;; jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike");
;; Set<String> sose = jedis.zrange("sose", 0, -1);
;; List
;; jedis.lpush("queue#tasks", "firstTask");
;; jedis.lpush("queue#tasks", "secondTask");
;; String task = jedis.rpop("queue#tasks");
;; Sets
;; jedis.sadd("nicknames", "nickname#1");
;; jedis.sadd("nicknames", "nickname#2");
;; jedis.sadd("nicknames", "nickname#1");
;; Set<String> nicknames = jedis.smembers("nicknames");
;; boolean exists = jedis.sismember("nicknames", "nickname#1");
(redis-zadd jedis
"sose"
0
"car")
(redis-zadd jedis
"sose"
0
"bike")
(redis-zrange jedis "sose" 0 -1)
(redis-lpush jedis "queue#task" ["firstTask"])
(redis-lpush jedis "queue#task" ["secondTask"])
(redis-rpop jedis "queue#task")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment