search: keywords: ['lesson 8', '8', 'redis', 'database', 'fast database', 'lettuce']
Lesson 8. Integrating with Redis
Hi! Long time I haven't posted lessons. Sorry for that:) Today we are integrating our
highload bot with lighting fast database called Redis. I am using it for data that needs quick access.
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>Latest</version> </dependency>
Then, you need to connect to Redis:
RedisClient redisClient; StatefulRedisConnection<String, String> redisConnection; RedisCommands<String, String> syncCommands; redisClient = RedisClient.create("redis://localhost:6379/0"); // Format: redis://ip:post/dbNumber redisConnection = redisClient.connect(); syncCommands = this.redisConnection.sync();
And thats all! Now you can execute commands like that:
syncCommands.get("key"); syncCommands.set("key", "value"); syncCommands.lrange("key", 0, -1);
Also, don't forget to close connection with Redis when you done your work:
Very short lesson, but I think useful :D Thanks for your time, hope to see you soon!