Results 1 to 2 of 2

Thread: ###Spring-data-redis caching support is not able to store object to the Redis server?

  1. #1
    Join Date
    Jun 2012
    Posts
    1

    Exclamation ###Spring-data-redis caching support is not able to store object to the Redis server?

    Hi Guys,

    I'm going to use the Redis Server as my caching system in my project, but I found that when I use the @cachable annotation to store / fetch the cache for a method result, but the problem is I'm only able to store / fetch a String value from Redis, but the same code is able to store / fetch Object from Ehcache.

    NOT sure if it's a bug, but I found that the Spring-data-redis caching support is not able to store object to the Redis server? Or it's not able to get the value correctly from the redis server, it just doesn't work, could anyone tell me how could I get it work correctly with Redis. (store & fetch Object). Thanks a lot!

    Below is my testing code.

    CODE############################################## ########

    @Cacheable(value = "testCache2")
    public User getUser2(String email){
    System.out.println("ad");
    return userMapper.getUserByEmail(email);
    }

    CONFIG############################################ ######


    <cache:annotation-driven />

    <bean id="connectionFactory"
    class="org.springframework.data.redis.connection.j edis.JedisConnectionFactory"
    p:host-name="${redis.host}" port="${redis.port}" password="${redis.pass}" />

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringR edisTemplate"
    p:connection-factory-ref="connectionFactory" />

    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisC acheManager"
    c:template-ref="redisTemplate" />




    EXCEPTION #################################

    java.lang.ClassCastException: java.lang.String cannot be cast to com.dr.rev.community.domain.User
    at com.dr.rev.community.action.UserAction$$EnhancerBy CGLIB$$e6c366b5.getUser2(<generated>)
    at com.dr.rev.community.persitence.UserMapperTest.tes tGetUserByUsername2(UserMapperTest.java:91)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.springframework.test.context.junit4.statements .RunBeforeTestMethodCallbacks.evaluate(RunBeforeTe stMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements .RunAfterTestMethodCallbacks.evaluate(RunAfterTest MethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements .SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:222)
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:300)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 )
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    The objects that you want to store in the cache need to be serialized somehow. In your case, you are using StringRedisTemplate (which reads and writes only Strings) and pass a User (the return value of you method) to it which results in the exception above.
    Use RedisTemplate with the JdkSerializer (or pick any other serialization protocol).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •