Redis: Difference between revisions
Jump to navigation
Jump to search
(→Pubsub) |
|||
Line 97: | Line 97: | ||
|} | |} | ||
==Spring Data Redis== | |||
<source lang="java"> | |||
package org.chorke.academia.beans.redis; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.data.redis.core.RedisTemplate; | |||
import org.springframework.scheduling.annotation.Scheduled; | |||
import org.springframework.stereotype.Component; | |||
import java.util.Date; | |||
import java.util.concurrent.TimeUnit; | |||
@Component | |||
public class ExpireKeyImpl { | |||
private static final Logger LOG = LoggerFactory.getLogger(ExpireKeyImpl.class); | |||
@Autowired | |||
private RedisTemplate<String, Object> redisTemplate; | |||
private String hashKey = new java.util.UUID(new Date().getTime(), 0).toString(); | |||
private String key = "name"; | |||
@Scheduled(fixedRate = 20000) | |||
public void expireKeyValueWrite() { | |||
String value = "CoverPlus"; | |||
redisTemplate.opsForHash().put(key, hashKey, value); | |||
redisTemplate.expire(key, 15, TimeUnit.SECONDS); | |||
LOG.info("Expiry-> PUT: Key: {}, Hash: {} Value: {}", key, hashKey, value); | |||
} | |||
@Scheduled(fixedRate = 5000) | |||
public void expiryKeyValueRead() { | |||
Object value = redisTemplate.opsForHash().get(key, hashKey); | |||
LOG.info("Expiry-> GET: Key: {}, Hash: {} Value: {}", key, hashKey, value); | |||
} | |||
} | |||
</source> | |||
== References == | == References == |
Revision as of 20:07, 31 May 2022
sudo apt install redis-server
sudo vim /etc/redis/redis.conf
Redis Config
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
sudo mkdir /var/run/redis
sudo chown -R redis:redis /var/run/redis
sudo systemctl restart redis-server
sudo systemctl status redis-server
###################<OR>###################
sudo vim /etc/systemd/system/redis.service
[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"
PIDFile=/run/redis/redis-server.pid
sudo systemctl daemon-reload sudo systemctl restart redis-server sudo systemctl status redis-server
Commander
# docker-compose.yml
version: "3.9"
services:
redis:
image: redis:latest
container_name: redis
ports:
- 127.20.22.10:6379:6379
networks:
redis:
aliases:
- redis.dev.chorke.org
redis-commander:
image: rediscommander/redis-commander:latest
container_name: redis-commander
depends_on:
- redis
environment:
- REDIS_HOSTS=redis:redis
ports:
- 127.20.22.10:8081:8081
networks:
redis:
aliases:
- cli.redis.dev.chorke.org
networks:
redis:
name: redis
Pubsub
redis-cli
SUBSCRIBE pubsub:queue
:'
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "pubsub:queue"
3) (integer) 1
1) "message"
2) "pubsub:queue"
3) "Hello"
'
|
redis-cli
PUBLISH pubsub:queue Hello
|
Spring Data Redis
package org.chorke.academia.beans.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Component
public class ExpireKeyImpl {
private static final Logger LOG = LoggerFactory.getLogger(ExpireKeyImpl.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private String hashKey = new java.util.UUID(new Date().getTime(), 0).toString();
private String key = "name";
@Scheduled(fixedRate = 20000)
public void expireKeyValueWrite() {
String value = "CoverPlus";
redisTemplate.opsForHash().put(key, hashKey, value);
redisTemplate.expire(key, 15, TimeUnit.SECONDS);
LOG.info("Expiry-> PUT: Key: {}, Hash: {} Value: {}", key, hashKey, value);
}
@Scheduled(fixedRate = 5000)
public void expiryKeyValueRead() {
Object value = redisTemplate.opsForHash().get(key, hashKey);
LOG.info("Expiry-> GET: Key: {}, Hash: {} Value: {}", key, hashKey, value);
}
}
References
| ||