This project demonstrates how to interact with a Redis instance using Java to store and retrieve a timestamp (lastSync). The project includes a Docker Compose setup to run a Redis instance with persistent storage, along with a Java program that retrieves and updates the lastSync timestamp.
- docker-compose.yml: A Docker Compose configuration to spin up a Redis container with persistent storage.
- Main.java: A Java program that connects to the Redis container, retrieves the
lastSynckey, simulates some work, and updates thelastSynckey with the current timestamp.
- Docker Compose (2.33.0)
- Java (JDK 11 or higher)
- Redis
To run the Java program, follow these steps:
-
Clone or download this repository.
git clone git@gitlab.com:deroosean/helloredis.git cd helloredis -
Start Redis with Docker Compose
docker-compose up
-
Compile and run the Java program (Main.java) using your preferred IDE or the command line.
-
The program will:
- Retrieve the
lastSynctimestamp from Redis. - Simulate some work (by sleeping for 5 seconds).
- Update the
lastSynctimestamp in Redis with the current time.
- Retrieve the
-
Checking the Last Sync Key You can verify that the
lastSynckey is correctly set and updated by using the following command:docker exec -it redis-instance redis-cli GET lastSyncThis will show you the current value of the
lastSynckey stored in Redis.
When you run the Java program, it will print the following to the console:
Last sync happened on: 2025-03-12T14:04:24.232473
Setting new last sync to: 2025-03-12T14:05:29.123456
- Redis: A fast, in-memory key-value store that is used for storing and retrieving data.
- Jedis: A Java client for Redis that allows easy interaction with Redis from Java applications.