Redis: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(37 intermediate revisions by the same user not shown) | |||
Line 40: | Line 40: | ||
sudo systemctl restart redis-server | sudo systemctl restart redis-server | ||
sudo systemctl status redis-server | sudo systemctl status redis-server | ||
== Commander == | |||
<source lang="properties"> | |||
# 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 | |||
</source> | |||
== Pubsub == | |||
{| | |||
|valign='top'| | |||
<source lang="bash"> | |||
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" | |||
' | |||
</source> | |||
|valign='top'| | |||
<source lang="bash"> | |||
redis-cli | |||
PUBLISH pubsub:queue Hello | |||
</source> | |||
|} | |||
==Expiry Key== | |||
<source lang="bash"> | |||
redis-cli | |||
:' | |||
SET expiryNameKey Academia EX 5 | |||
TTL expiryNameKey | |||
GET expiryNameKey | |||
SET expiryNameKey Academia | |||
EXPIRE expiryNameKey 5 | |||
TTL expiryNameKey | |||
GET expiryNameKey | |||
EXPIREAT expiryNameKey 1665396610 | |||
TTL expiryNameKey | |||
GET expiryNameKey | |||
PERSIST expiryNameKey | |||
TTL expiryNameKey | |||
GET expiryNameKey | |||
' | |||
</source> | |||
==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.UUID; | |||
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 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> | |||
==Spring Redis Reactive== | |||
<source lang="java"> | |||
package org.chorke.academia.beans.redis; | |||
import org.chorke.academia.dto.SiteQueryDto; | |||
import org.chorke.academia.utility.UUIDUtil; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.data.redis.core.ReactiveRedisTemplate; | |||
import org.springframework.scheduling.annotation.Scheduled; | |||
import org.springframework.stereotype.Component; | |||
import reactor.core.publisher.Mono; | |||
import javax.annotation.PostConstruct; | |||
import java.time.Duration; | |||
@Component | |||
public class ReactiveExpireKeyImpl { | |||
private static final Logger LOG = LoggerFactory.getLogger(ReactiveExpireKeyImpl.class); | |||
@Autowired | |||
private ReactiveRedisTemplate<String, Object> reactiveRedisTemplate; | |||
private SiteQueryDto siteQueryDto; | |||
private String hashKey; | |||
private String key; | |||
@PostConstruct | |||
private void init() { | |||
siteQueryDto = SiteQueryDto.builder().code("1000").name("Academia").url("https://chorke.org/academia").build(); | |||
hashKey = UUIDUtil.uuid().toString(); | |||
key = "name"; | |||
} | |||
@Scheduled(fixedRate = 20000) | |||
public void expireKeyValueWrite() { | |||
reactiveRedisTemplate.opsForHash().put(key, hashKey, siteQueryDto).subscribe(); | |||
reactiveRedisTemplate.expire(key, Duration.ofSeconds(15)).subscribe(); | |||
LOG.info("Expiry-> PUT: Key: {}, Hash: {} Value: {}", key, hashKey, siteQueryDto); | |||
} | |||
@Scheduled(fixedRate = 5000) | |||
public void expiryKeyValueRead() { | |||
Mono<Object> value = reactiveRedisTemplate.opsForHash().get(key, hashKey); | |||
LOG.info("Expiry-> GET: Key: {}, Hash: {} Value: {}", key, hashKey, value.block()); | |||
} | |||
} | |||
</source> | |||
==Camel Spring-Redis== | |||
<source lang="java"> | |||
public interface RedisSubscriber { | |||
String SPRING_REDIS_SUBSCRIBE_URI = "spring-redis://{{spring.redis.host}}:{{spring.redis.port}}?serializer=#redisSerializer&command=SUBSCRIBE&channels=/redis/topic/pub"; | |||
// String SPRING_REDIS_SUBSCRIBE_URI = "spring-redis://?connectionFactory=#redisConnectionFactory&serializer=#redisSerializer&command=SUBSCRIBE&channels=/redis/topic/pub"; | |||
} | |||
public interface RedisPublisher { | |||
String SPRING_REDIS_PUBLISH_URI = "spring-redis://{{spring.redis.host}}:{{spring.redis.port}}?redisTemplate=#redisTemplate&command=PUBLISH"; | |||
// String SPRING_REDIS_PUBLISH_URI = "spring-redis://?connectionFactory=#redisConnectionFactory&redisTemplate=#redisTemplate&command=PUBLISH"; | |||
} | |||
</source> | |||
== References == | == References == | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
* [[ | * [[Docker Compose]] | ||
* [[ | * [[Ubuntu/GraalVM]] | ||
* [[Minikube]] | * [[Minikube]] | ||
* [[Quarkus]] | * [[Quarkus]] | ||
Line 56: | Line 240: | ||
| valign="top" | | | valign="top" | | ||
* [[ | * [https://medium.com/nerd-for-tech/event-driven-architecture-with-redis-streams-using-spring-boot-a81a1c9a4cde Event-Driven Architecture With Redis Streams] | ||
* [https://www.baeldung.com/spring-data-redis-pub-sub Redis PubSub with Spring Data Redis] | |||
* [https://unix.stackexchange.com/questions/200239/ SSH Sessions Prevent From Freezing] | * [https://unix.stackexchange.com/questions/200239/ SSH Sessions Prevent From Freezing] | ||
* [ | * [https://docs.servicestack.net/redis-mq Redis MQ Implement with C#] | ||
* [ | * [https://redis.io/docs/manual/config/ Redis Cache Configuration] | ||
* [https://redis.io/docs/manual/client-side-caching/ Redis Client Side Caching] | |||
* [https://redis.io/docs/manual/data-types/data-types-tutorial/ Redis Data Types Tutorial] | |||
* [https://redis.io/docs/manual/data-types/ Redis Data Types] | |||
* [https://redis.io/docs/manual/pubsub/ Redis Pub/Sub] | |||
* [https://redis.io/docs/manual/cli/ Redis CLI] | |||
| valign="top" | | | valign="top" | | ||
* [https://redis.io/docs/manual/sentinel/ High availability with Redis Sentinel] | |||
* [https://redis.io/docs/manual/keyspace-notifications/ Redis Keyspace Notifications] | |||
* [https://redis.io/docs/manual/scaling/ Scaling with Redis Cluster] | |||
* [https://redis.io/docs/manual/programmability/ Redis Programmability] | |||
* [https://redis.io/docs/manual/transactions/ Redis Transactions] | |||
* [https://redis.io/docs/manual/eviction/ Redis Key Eviction] | |||
* [https://redis.io/docs/manual/persistence/ Redis persistence] | |||
* [https://redis.io/docs/manual/pipelining/ Redis Pipelining] | |||
* [https://redis.io/docs/manual/data-types/streams/ Redis Streams] | |||
* [https://redis.io/docs/manual/security/ Redis security] | |||
|- | |- | ||
Line 78: | Line 268: | ||
|- | |- | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/5606106/ Redis Keys & String Values are Limited to 512 MiB] | |||
* [https://spring.getdocs.org/en-US/spring-data-docs/spring-data-redis/reference/redis/pubsub.html Spring Data Redis Messaging (Pub/Sub)] | |||
* [https://stackoverflow.com/questions/58690313/ Redis how to store values as JSON] | |||
* [https://developpaper.com/spring-data-redis-best-practices/ Spring Data Redis Best Practices] | |||
* [https://www.baeldung.com/spring-data-redis-reactive Spring Data Redis Reactive] | |||
* [https://redis.io/commands/expire/ Redis Set Expiry on Key] | |||
* [https://redis.io/docs/manual/troubleshooting/ Troubleshooting Redis] | |||
* [https://redis.io/docs/manual/admin/ Redis Administration] | |||
* [https://redis.io/commands/expire/ EXPIRE key seconds] | |||
* [https://www.epochconverter.com Epoch Converter] | |||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/18698802/ Camel » Spring-Redis » Wrong serialization] | |||
* [https://stackoverflow.com/questions/23119359/ Camel » Spring-Redis » Set Key/Value] | |||
* [[Apache Camel]] | |||
* [[Jasypt]] | |||
| valign="top" | | | valign="top" | | ||
|} | |} |
Latest revision as of 20:07, 28 August 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
|
Expiry Key
redis-cli
:'
SET expiryNameKey Academia EX 5
TTL expiryNameKey
GET expiryNameKey
SET expiryNameKey Academia
EXPIRE expiryNameKey 5
TTL expiryNameKey
GET expiryNameKey
EXPIREAT expiryNameKey 1665396610
TTL expiryNameKey
GET expiryNameKey
PERSIST expiryNameKey
TTL expiryNameKey
GET expiryNameKey
'
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.UUID;
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 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);
}
}
Spring Redis Reactive
package org.chorke.academia.beans.redis;
import org.chorke.academia.dto.SiteQueryDto;
import org.chorke.academia.utility.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import javax.annotation.PostConstruct;
import java.time.Duration;
@Component
public class ReactiveExpireKeyImpl {
private static final Logger LOG = LoggerFactory.getLogger(ReactiveExpireKeyImpl.class);
@Autowired
private ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
private SiteQueryDto siteQueryDto;
private String hashKey;
private String key;
@PostConstruct
private void init() {
siteQueryDto = SiteQueryDto.builder().code("1000").name("Academia").url("https://chorke.org/academia").build();
hashKey = UUIDUtil.uuid().toString();
key = "name";
}
@Scheduled(fixedRate = 20000)
public void expireKeyValueWrite() {
reactiveRedisTemplate.opsForHash().put(key, hashKey, siteQueryDto).subscribe();
reactiveRedisTemplate.expire(key, Duration.ofSeconds(15)).subscribe();
LOG.info("Expiry-> PUT: Key: {}, Hash: {} Value: {}", key, hashKey, siteQueryDto);
}
@Scheduled(fixedRate = 5000)
public void expiryKeyValueRead() {
Mono<Object> value = reactiveRedisTemplate.opsForHash().get(key, hashKey);
LOG.info("Expiry-> GET: Key: {}, Hash: {} Value: {}", key, hashKey, value.block());
}
}
Camel Spring-Redis
public interface RedisSubscriber {
String SPRING_REDIS_SUBSCRIBE_URI = "spring-redis://{{spring.redis.host}}:{{spring.redis.port}}?serializer=#redisSerializer&command=SUBSCRIBE&channels=/redis/topic/pub";
// String SPRING_REDIS_SUBSCRIBE_URI = "spring-redis://?connectionFactory=#redisConnectionFactory&serializer=#redisSerializer&command=SUBSCRIBE&channels=/redis/topic/pub";
}
public interface RedisPublisher {
String SPRING_REDIS_PUBLISH_URI = "spring-redis://{{spring.redis.host}}:{{spring.redis.port}}?redisTemplate=#redisTemplate&command=PUBLISH";
// String SPRING_REDIS_PUBLISH_URI = "spring-redis://?connectionFactory=#redisConnectionFactory&redisTemplate=#redisTemplate&command=PUBLISH";
}
References
| ||