Install Redis on Ubuntu 22.04

sudo apt install redis-server
sudo nano /etc/redis/redis.conf

Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised directive is set to no by default. Since you are running Ubuntu, which uses the systemd init system, change this to systemd

. . .

# 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 systemctl restart redis.service

References
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-redis-on-ubuntu-22-04

Install Redis on Windows using WSL

Turn on Windows Subsystem for Linux

Open PowerShell as Administrator and run this command to enable Windows Subsystem for Linux (WSL):

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Launch Microsoft Windows Store and then search for Ubuntu, or your preferred distribution of Linux, and download the latest version.

Install Redis server

sudo apt-add-repository ppa:redislabs/redis
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install redis-server

Restart the Redis server

sudo service redis-server restart

Verify if your Redis server is running

 $ redis-cli
 127.0.0.1:6379> set user:1 "Jane"
 127.0.0.1:6379> get user:1
"Jane"

Stop the Redis Server

sudo service redis-server stop

References
https://developer.redis.com/create/windows/
https://redis.io/docs/install/install-redis/install-redis-on-windows/

Configure and Use Spring Data Redis

Configuration

@Configuration
public class RedisConfiguration {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        ConfigFile configFile = new ConfigFile();
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(configFile.getRedisHost());
        configuration.setPassword(configFile.getRedisPassword());
        return new LettuceConnectionFactory(configuration);
    }

    @Bean
    public StringRedisTemplate redisTemplate() {
        return new StringRedisTemplate(redisConnectionFactory());
    }
}

You can use RedisTemplate instead of StringRedisTemplate.

Redis Repository

@RedisHash("Student")
public class Student implements Serializable {
  
    public enum Gender { 
        MALE, FEMALE
    }

    @Id private String id;
    private String name;
    private Gender gender;
    private int grade;
    // ...
}

The Spring Data Repository

@Repository
public interface StudentRepository extends CrudRepository<Student, String> {}

Data Access Using StudentRepository

Saving a New Student Object

Student student = new Student(
  "Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.save(student);

Retrieving an Existing Student Object

Student retrievedStudent = 
  studentRepository.findById("Eng2015001").get();

Updating an Existing Student Object

retrievedStudent.setName("Richard Watson");
studentRepository.save(student);

Deleting Existing Student Data

studentRepository.deleteById(student.getId());

Find All Student Data

Student engStudent = new Student(
  "Eng2015001", "John Doe", Student.Gender.MALE, 1);
Student medStudent = new Student(
  "Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
studentRepository.save(engStudent);
studentRepository.save(medStudent);

References
https://docs.spring.io/spring-data/redis/docs/current/reference/html/
https://www.baeldung.com/spring-data-redis-tutorial

Install Redis on Ubuntu 20.04

sudo apt install redis-server

Configure Redis

sudo nano /etc/redis/redis.conf

find the line specifying the supervised directive. By default, this line is set to no. However, to manage Redis as a service, set the supervised directive to systemd (Ubuntu’s init system).

Secure Redis

sudo nano /etc/redis/redis.conf

locate the requirepass directive under the SECURITY section and uncomment it (by removing #).Once you have uncommented the line, replace foobared with the password of your choice.

sudo systemctl restart redis.service

References
https://phoenixnap.com/kb/install-redis-on-ubuntu-20-04

Configure Spring Session with Redis

build.gradle

compile('org.springframework.session:spring-session')
compile('org.springframework.boot:spring-boot-starter-data-redis')

application.properties

spring.session.store-type=redis

spring.redis.host=localhost
#spring.redis.password=secret
spring.redis.port=6379