Search code examples
javaehcacheserializablespymemcached

Spymemcached - Non-serializable object for Authentication object


I'm trying out Spymemcached for java.

I have successfully created a MemCachedClient and I'm trying to do this:

mc.set(token, 0, authentication);

Token is a string and the authentication object is an interface of Spring Authentication which extends Serializable: http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/core/Authentication.html

This throws a non-serializable object exception:

{
  "timestamp": 1438463381311,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.IllegalArgumentException",
  "message": "Non-serializable object",
  "path": "/auth"
}

When I tried to save the token and the authentication object in Ehcache, it works perfectly and both the key and the value have also got to be serializable:

Ehcache.put(new Element(token, authentication));

Does anyone know why it doesn't work with spymemcached? I'm using:

compile 'net.spy:spymemcached:2.12.0'

Solution

  • OK - I figured it out. For some reason one of my classes that flowed into the Authentication object was not serializable. To make Authentication serializable, all the classes that goes into my Authentication object needs to be serializable.

    I just implemented Serializable for that specific object class and spymemcached worked.