skip to Main Content

I’m using this client in my Spring boot project to connect to my memcached instance running on my cluster.

Everything works fine BUT so far I could only set on expiration date for all my caches, which is not convenient for me now, I want to able to set custom expiration dates like it’s possible to do on memcached for Appengine.

Anybody has any idea? the client doesn’t seem that flexible.

2

Answers


  1. This is because Spring does not have native support for custom expiration. I suggest you use simple-spring-memcached with Spring Boot to enable custom expiration. If you use this library you have two options:

    1. Use the annotations provided by simple-spring-memcached. They support custom expiration.
    2. Use the ExtendedSSMCacheManager provided by simple-spring-memcached to integrate with Spring Boot’s native caching. It allows you to set expiration as part of the cache name like so:

      @Cacheable("default#3600")   
      public ComplexSerializableResult compute(Long input) {
          // ...
          return result;   
      }
      

      Here default is the cache name and the expiration is set to 3600 seconds.

    For more information on how to set up simple-spring-memcached with Spring Boot see the SSM wiki or MemCachier’s Spring Boot documentation.

    For step by step instructions on how to use simple-spring-memcached with Spring Boot see this tutorial. It is created for the Heroku platform but works independently from Heroku.

    Login or Signup to reply.
  2. You could set custom expiration per cache name also with memcached-spring-boot library. You can just do it through memcached.cache.expirations configuration property. E.g.:

     memcached.cache:
         servers: example1.com:11211,example2.com:11211
         mode: static
         expirations: 86400, cache_name1:3600, cache_name2:108000 
    

    Here 86400 represents global expiration (used for all caches). And in case you want to have different expiration value per cache, you can set it up as cache_name1:3600 (cache with name cache_name1 will expire in 3600 seconds).

    In this case your cache configuration is outside the source code, so it would be easier for you to configure different expiration per different environment (e.g. dev, prod).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search