Skip to content

Instantly share code, notes, and snippets.

@diegolirio
Last active June 6, 2020 20:31
Show Gist options
  • Save diegolirio/fcd6642b48509a4d687b25aa3027030a to your computer and use it in GitHub Desktop.
Save diegolirio/fcd6642b48509a4d687b25aa3027030a to your computer and use it in GitHub Desktop.

Redis

Install

  1. Você pode baixar o Redis no link -> https://redis.io/download
  2. Também pode provisionar utilizando docker -> https://hub.docker.com/_/redis/
curl http://download.redis.io/releases/redis-3.0.6.tar.gz  -o redis-3.0.6.tar.gz
tar xzf redis-3.0.6.tar.gz
cd redis-3.0.6
make

Em caso de erro com make ou gcc, instale os mesmos... sudo apt-get install make sudo apt-get install gcc

Get Started

cd src/
./redis-server

Em outra janela execute o comando abaixo:

./redis-cli
   
127.0.0.1:6379> SET count_customers 22
127.0.0.1:6379> GET count_customers

Para Excluir

127.0.0.1:6379> DEL count_customers

Redis Data Type

  • STRINGs
  • LISTs
  • SETs
  • HASHes => @RedisHash
  • ZSETs

String Data Redis

https://github.com/diegolirio/demos-spring-boot/tree/master/demo-spring-boot-2-redis

  • Maven
   <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
    <dependency>
  • Product.java
@RedisHash("product")
public class Product {
    @Id
    private String id;
    private String description;
    private double price;
}
  • Redis
127.0.0.1:6379> KEYS *

Output.

  1. "product"
  2. "product:3b4e5a1f-3793-4e5d-8338-90325009ed0a"
127.0.0.1:6379> HGETALL product:3b4e5a1f-3793-4e5d-8338-90325009ed0a   
  1. "_class"
  2. "io.github.diegolirio.demospringboot2redis.customer.Product"
  3. "id"
  4. "3b4e5a1f-3793-4e5d-8338-90325009ed0a"
  5. "description"
  6. "NOTEBOOK DELL"
  7. "price"
  8. "1000.0"
127.0.0.1:6379> HGET product:3b4e5a1f-3793-4e5d-8338-90325009ed0a description

"NOTEBOOK DELL"

127.0.0.1:6379> EXPIRE product:3b4e5a1f-3793-4e5d-8338-90325009ed0a 5
127.0.0.1:6379> TTL "product:3b4e5a1f-3793-4e5d-8338-90325009ed0a"
(integer) 2
@diegolirio
Copy link
Author

diegolirio commented Feb 12, 2020

LPUSH carros_novos "Fiat Uno"
LPUSH carros_novos "Honda Fit"
LPUSH carros_novos "Hyundai i30"
LPUSH carros_novos "Chevrolet Onix"
LINDEX carros_novos 0
LLEN carros_novos
LRANGE carros_novos 1 2
LLEN carros_novos
LTRIM carros_novos 0 2

@diegolirio
Copy link
Author

Redis as DB

127.0.0.1:6379> CONFIG GET databases
1) "databases"
2) "16"
127.0.0.1:6379>

@diegolirio
Copy link
Author

127.0.0.1:6379> SELECT 5
OK
127.0.0.1:6379[5]> INFO Keyspace
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
127.0.0.1:6379[5]> 
127.0.0.1:6379[5]> 
127.0.0.1:6379[5]> Set produto "TV Samsung 42"
OK
127.0.0.1:6379[5]> INFO Keyspace
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
db5:keys=1,expires=0,avg_ttl=0
127.0.0.1:6379[5]> 

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

./redis-server --port 6380 --slaveof 127.0.0.1 6379

@diegolirio
Copy link
Author

image

image

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

image

@diegolirio
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment