spymemcached

spymemcached

Provides memcached access via SpyMemcached

exports

  • A MemcachedClient service

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-spymemcached</artifactId>
  <version>1.6.6</version>
</dependency>

usage

 memcached.server = "localhost:11211"
{
  use(new SpyMemcached());

  get("/", req -> {
    MemcachedClient client = require(MemcachedClient.class);
    client.set("foo", 60, "bar");
    return client.get("foo");
  });
}

configuration

It is done via .conf file:

memcached.protocol = binary

or programmatically:

{
  use(new SpyMemcached()
   .doWith(builder -> {
     builder.setProtocol(Protocol.BINARY);
   })
 );
}

spymemcached session store

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-spymemcached</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new SpyMemcached());

  session(SpySessionStore.class);

  get("/", req -> {
   req.session().set("name", "jooby");
  });
}

The name attribute and value will be stored in Memcached.

Session are persisted using the default Transcoder.

options

timeout

By default, a Memcached session will expire after 30 minutes. Changing the default timeout is as simple as:


# 8 hours 
session.timeout = 8h

# 15 seconds 
session.timeout = 15

# 120 minutes 
session.timeout = 120m

# no timeout 
session.timeout = -1

key prefix

Default Memcached key prefix is sessions:. Sessions in Memcached will looks like: sessions:ID

It’s possible to change the default key setting the memcached.sesssion.prefix property.

Happy coding!!

memcached.conf

These are the default properties for spymemcached:

memcached {

  authWaitTime = 1s

  daemon = false

  failureMode = redistribute

  locator = ARRAY_MOD

  maxReconnectDelay = 30s

  protocol = text

  shutdownTimeout = -1

}

#default HTTP session settings 
memcached.session.prefix = "sessions:"

memcached.session.timeout = ${session.timeout}