Skip to content

SingleApi

Internet, programming, artificial intelligence

Menu
  • Home
  • About
  • My Account
  • Registration
Menu

Spring Data Redis and LUA

Posted on May 19, 2019

In my recent project, I had a situation when I need to store data under two different keys. I didn’t want to duplicate content because of difficulties with changes synchronization. I found that it’s possible to do this with LUA script, but not without any cost.

Background

I use Spring Data with Redis support so what can be done is:

RedisTemplate<String, T> redisTemplate;
HashOperations<String, String, T> hashOps = redisTemplate.opsForHash();
hashOps.put(getKey(), key2, value);
hashOps.get(getKey(), key2);

but when you have situation like compositeKey -> id and id -> serialized data you have to make 2 calls to get data.

Solution

With support of LUA scripting it can be done with one call, and another but without sending partial data on network.

local x = redis.call('hget', KEYS[1], KEYS[2]); if not x then return nil; else return redis.call('hget', KEYS[1], x); end;

this query will work fine if used in redis-cli, but we will get into troubles if we want to use it with mix of hashOps from first code excerpt and execute method, because of Spring object serialization. So my solution was to write all code as LUA scripts, then all keys will be regular strings.

DefaultRedisScript ADD_ENTRY_REDIS_SCRIPT = new DefaultRedisScript("redis.call('hset', KEYS[1], KEYS[2], ARGV[1]);");
DefaultRedisScript GET_ENTRY_REDIS_SCRIPT
            = new DefaultRedisScript("local x = redis.call('hget', KEYS[1], KEYS[2]); if not x then return nil; else return redis.call('hget', KEYS[1], x); end;");
redisTemplate.execute(ADD_ENTRY_REDIS_SCRIPT, new JdkSerializationRedisSerializer(), new JdkSerializationRedisSerializer(),
                Lists.newArrayList(key1, compositeKey), /* String */ key2);
redisTemplate.execute(ADD_ENTRY_REDIS_SCRIPT, new JdkSerializationRedisSerializer(), new JdkSerializationRedisSerializer(),
                Lists.newArrayList(key1, key2), /* List<SomeDTO>(...) */ value);
(List< SomeDTO >) redisTemplate.execute(GET_ENTRY_REDIS_SCRIPT, new JdkSerializationRedisSerializer(),
                new JdkSerializationRedisSerializer(), Lists.newArrayList(key1, key2));

For deserialization was also required: GET_ENTRY_REDIS_SCRIPT.setResultType(byte[].class); before using it, and as DefaultRedisScript should be singleton I’ve placed it in constructor.

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Docker Desktop MCP toolkit
  • Hunyuan 3D 2.1, nVidia PartPacker, Sparc3D
  • HiDream
  • Jules
  • IntelliJ IDEA with free AI

Recent Comments

  • adrian on Kokoro TTS Model, LLM Apps Curated List
  • adrian on Repo Prompt and Ollama
  • adrian on A Content Creation Assistant

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • November 2023
  • May 2022
  • March 2022
  • January 2022
  • August 2021
  • November 2020
  • September 2020
  • April 2020
  • February 2020
  • January 2020
  • November 2019
  • May 2019
  • February 2019

Categories

  • AI
  • Apple Intelligence
  • Claude
  • Cursor
  • DeepSeek
  • Gemini
  • Google
  • Graphics
  • IntelliJ
  • Java
  • LLM
  • Made in Poland
  • MCP
  • Meta
  • Open Source
  • OpenAI
  • Programming
  • Python
  • Repo Prompt
  • Technology
  • Uncategorized
  • Work

agents ai apple apps automation blender cheatsheet china claude comfyui deepseek docker draw things flux gemini google hidream hobby huggingface hugging face java langchain langchain4j llama llm mcp meta mlx movies news nvidia ollama openai personal thoughts quarkus release repo prompt speech-to-speech spring stable diffusion tts whisper work

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Terms & Policies

  • Privacy Policy
©2025 SingleApi | Design: Newspaperly WordPress Theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT