Search code examples
javaredisspring-dataspring-data-redis

Does Spring Data Redis support Java streams?


I can't find it explicitly in https://docs.spring.io/spring-data/redis/docs/current/reference/html/ but I was wondering if Redis supports Java streams so we can do

Stream<SomeDataStruct> findByServerIdAndSiteIdAndUserId(String serverId, String siteId, int userId);

And it will free up the resources as needed?

I know JPA does support streams, though I had to explicitly do a entityManager.detach(element) in order to prevent the entity manager taking up a lot of memory.

I was thinking it would be in their spring-data-commons but https://spring.io/projects/spring-data#learn do not have any links just labels.


Solution

  • I was able to get to the point of my code to verify this

    Adding

    Stream<ArtifactOnKafka> streamByServerIdAndSiteId(String serverId, String siteId);
    

    And accessing it gives

    java.lang.ClassCastException: class cc.ArtifactOnKafka cannot be cast to class java.util.stream.Stream (cc.ArtifactOnKafka is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @2cfb4a64; java.util.stream.Stream is in module java.base of loader 'bootstrap')
    

    So it appears it is not supported.

    To fake it

        default Stream<ArtifactOnKafka> streamByServerIdAndSiteId(String serverId, String siteId) {
            return findByServerIdAndSiteId(serverId, siteId).stream();
        }