Feature Enrichment with Spring Data Redis and Lettuce

Image for post
Image for post

Lettuce by far is the best Redis client out there for Java. In the java world, Annotations and Mappings are pretty popular. I build services using these concepts before however they were always under the service boundaries, never as shared libraries in order to avoid coupling. So today I record a video showing how we can use this feature and also extend the functionality to add custom enriched behavior. We will be using Spring Boot 2, Spring Data Redis, and Lettuce as redis Client. We also will need to have redis running either as standalone / cluster or running on docker. We will create custom annotations in order to describe the behavior we want. I need to say that some people love and other people hate annotations, I have mixed feelings. Often for those that don’t like it — check this out. Besides that, let’s take a look. So Let’s get started.

Video

Code

Model

Our model is pretty simple. Simple Person POJO with Getters/Setters, toString, equals/hashCode all generated by Idea. You can see there is a custom annotation called UpperCase. The idea is to add custom features enrichment and make sure some fields are stored in the Upper case.

Annotation

Now let’s look at the annotation.

Custom Repository

Here is where the magic happens. I wrapped all CurdRepository operations and I’m delegating to the proper CrudRepository implementation at the end of the day. However I’, also I have a reflection code that adds the feature enrichment I want based on the existence of the UpperCase annotation at the Entity fields.

The complete code is here.

Cheers,
Diego Pacheco

Originally published at http://diego-pacheco.blogspot.com on May 10, 2020.

Written by

Brazilian, Software Architect, SWE(Java, Scala, Rust, Go) SOA & DevOps expert, Author. Working with EKS/K8S. diegopacheco.github.io (Opinions on my own)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store