Skip to content

Class with @RedisHash and byte[] property is turned into Map<String, Byte> #1981

Closed
@bergerst

Description

@bergerst

I discovered that if you define a class with @RedisHash and a byte[] property, one key-value pair is stored for each byte.

Here's some example code:

@RedisHash("myclass")
@lombok.Data
public class MyClass {
  @Id
  private String id;
  private byte[] property;

  public MyClass(String id) {
    this.id = id;
    property = new byte[4000];
    Random rd = new Random();
    rd.nextBytes(property);
  }
}

When an object of this class is stored in Redis using a CrudRepository<MyClass, String>, the Redis content will look like this:

property.[3018] = 50
property.[298] = 156
property.[1892] = 2
...

When you then use findById on the repository, Bucket.extract() will be called by MappingRedisConverter.readCollectionOrArray() for each key, leading to about 16 million String.startsWith() calls.

In my opinion, the byte[] should be stored as a String value, not as a Map<String, Byte>.

Replacing the byte[] with a String reduced the run time of my webservice operation from 1200 ms to 45 ms.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions