Пример #1
0
 @Override
 public void setSessionAttribute(final String name, final Object value) {
   Session session = req.session();
   if (value == null) {
     session.unset(name);
   } else {
     session.set(name, objToStr(value));
   }
 }
  @Test
  public void create() throws Exception {
    Map<String, String> attrs = ImmutableMap.of("x", "X");
    Map<String, String> attrsToSave =
        ImmutableMap.of(
            "x", "X",
            "_accessedAt", "2",
            "_createdAt", "1",
            "_savedAt", "3");
    new MockUnit(JedisPool.class, Session.class)
        .expect(
            unit -> {
              Session session = unit.get(Session.class);
              expect(session.id()).andReturn("1234");
              expect(session.attributes()).andReturn(attrs);
              expect(session.createdAt()).andReturn(1L);
              expect(session.accessedAt()).andReturn(2L);
              expect(session.savedAt()).andReturn(3L);
            })
        .expect(
            unit -> {
              Jedis jedis = unit.mock(Jedis.class);
              expect(jedis.hmset("sessions:1234", attrsToSave)).andReturn("sessions:1234");
              expect(jedis.expire("sessions:1234", 1800)).andReturn(1L);
              jedis.close();

              JedisPool pool = unit.get(JedisPool.class);
              expect(pool.getResource()).andReturn(jedis);
            })
        .run(
            unit -> {
              new RedisSessionStore(unit.get(JedisPool.class), "sessions", "30m")
                  .create(unit.get(Session.class));
            });
  }
Пример #3
0
 @Override
 public Object getSessionAttribute(final String name) {
   Session session = req.session();
   return strToObject(session.get(name).value(null));
 }