org.redisson.codec.JsonJacksonCodec Java Examples

The following examples show how to use org.redisson.codec.JsonJacksonCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultiCache.java    From mPaaS with Apache License 2.0 6 votes vote down vote up
/** 读 */
@SuppressWarnings("unchecked")
public <T> T get(String key) {
    Object value = null;
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        value = map.get(key);
        if (value != null) {
            return value == NULL ? null : (T) value;
        }
    }
    value = redisson.getBucket(key, JsonJacksonCodec.INSTANCE).get();
    if (map != null) {
        map.put(key, value == null ? NULL : value);
    }
    return (T) value;
}
 
Example #2
Source File: RedissonTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testNextResponseAfterDecoderError() throws Exception {
    Config config = new Config();
    config.useSingleServer()
            .setConnectionMinimumIdleSize(1)
            .setConnectionPoolSize(1)
          .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());

    RedissonClient redisson = Redisson.create(config);
    
    setJSONValue(redisson, "test1", "test1");
    setStringValue(redisson, "test2", "test2");
    setJSONValue(redisson, "test3", "test3");
    try {
        RBuckets buckets = redisson.getBuckets(new JsonJacksonCodec());
        buckets.get("test2", "test1");
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertThat(getStringValue(redisson, "test3")).isEqualTo("\"test3\"");
    
    redisson.shutdown();
}
 
Example #3
Source File: BaseMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 5000)
public void testDeserializationErrorReturnsErrorImmediately() throws Exception {
    RMap<String, SimpleObjectWithoutDefaultConstructor> map = getMap("deserializationFailure", new JsonJacksonCodec());
    Assume.assumeTrue(!(map instanceof RLocalCachedMap));
    SimpleObjectWithoutDefaultConstructor object = new SimpleObjectWithoutDefaultConstructor("test-val");

    Assert.assertEquals("test-val", object.getTestField());
    map.put("test-key", object);

    try {
        map.get("test-key");
        Assert.fail("Expected exception from map.get() call");
    } catch (Exception e) {
        e.printStackTrace();
    }
    destroy(map);
}
 
Example #4
Source File: UserRepository.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
/**
 * redission 作为二级缓存DAO层的案例
 * @param id
 * @return
 */
public String getUserCacheable(long id) {
    try{
        RBucket<String> rBucket = redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, id, JsonJacksonCodec.INSTANCE));
        if (rBucket.isExists()) {
            rBucket.expire(AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
            return rBucket.get();
        }
        String name = jdbcTemplate.queryForObject("select username from user where id = ?", String.class, new Object[]{id});
        if (Objects.nonNull(name)) {
            redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, id),JsonJacksonCodec.INSTANCE)
                    .setAsync(name, AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
        }
        return name;
    }catch(Throwable th){
        throw new RepositoryException(ErrorCode.DB_ERROR.getErrorCode(),ErrorCode.DB_ERROR.getErrorMsg(),th);
    }

}
 
Example #5
Source File: UserRepository.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
/**
 * redission 作为二级缓存DAO层的案例
 * @param itemId
 * @return
 */
public UserDO getGoodsCacheable(long itemId) {
    try{
        RBucket<UserDO> rBucket = redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, itemId, JsonJacksonCodec.INSTANCE));
        if (rBucket.isExists()) {
            rBucket.expire(AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
            return rBucket.get();
        }
        UserDO topicDO = getGoods(itemId);
        if (Objects.nonNull(topicDO)) {
            redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, itemId),JsonJacksonCodec.INSTANCE)
                    .setAsync(topicDO, AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
        }
        return topicDO;
    }catch(Throwable th){
        throw new RepositoryException(ErrorCode.DB_ERROR.getErrorCode(),ErrorCode.DB_ERROR.getErrorMsg(),th);
    }

}
 
Example #6
Source File: MultiCache.java    From mPass with Apache License 2.0 6 votes vote down vote up
/** 读 */
@SuppressWarnings("unchecked")
public <T> T get(String key) {
    Object value = null;
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        value = map.get(key);
        if (value != null) {
            return value == NULL ? null : (T) value;
        }
    }
    value = redisson.getBucket(key, JsonJacksonCodec.INSTANCE).get();
    if (map != null) {
        map.put(key, value == null ? NULL : value);
    }
    return (T) value;
}
 
Example #7
Source File: RedisIdentityFactoryTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Before
public void testBefore() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
    keys = redisson.getKeys();
    keys.flushdb();
}
 
Example #8
Source File: GlobalBloomFilterTestCase.java    From jstarcraft-nlp with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void beforeClass() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
}
 
Example #9
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #10
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #11
Source File: GlobalIdGeneratorUtil.java    From gpmall with Apache License 2.0 5 votes vote down vote up
public String getMaxSeq() throws ExecutionException, InterruptedException {
    List<Object> keys= Arrays.asList(keyName,incrby,generateSeq());
    RedissonScript rScript=(RedissonScript) redissonClient.getScript();
    //这里遇到一个bug,默认情况下使用evalSha,不加Codec属性时,会报错。这个错误很神奇。花了3个小时才搞定。
    Long seqNext=rScript.evalSha(RScript.Mode.READ_ONLY, JsonJacksonCodec.INSTANCE,sha1, RScript.ReturnType.VALUE,keys);
    return seqNext.toString();
}
 
Example #12
Source File: RedisQualityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
}
 
Example #13
Source File: RedisQuantityAttributeTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
}
 
Example #14
Source File: MultiCache.java    From mPass with Apache License 2.0 5 votes vote down vote up
/** 写,当key不存在时无效,并返回false */
@SuppressWarnings("unchecked")
public boolean hset(String key, String field, Object value) {
    Object map = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中判断key是否存在
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map == NULL) {
            // key不存在,写失败
            return false;
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = map != null || rmap.isExists();
    if (!exists) {
        if (cache != null) {
            cache.put(key, NULL);
        }
        return false;
    }
    // 写入数据
    rmap.put(field, value);
    if (cache != null) {
        if (map == null) {
            map = new HashMap<>(16);
            cache.put(key, map);
        }
        ((Map<String, Object>) map).put(field,
                value == null ? NULL : value);
    }
    return true;
}
 
Example #15
Source File: RedisTransactionManagerTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Before
public void testBefore() {
    // 注意此处的编解码器
    Codec codec = new JsonJacksonCodec();
    Config configuration = new Config();
    configuration.setCodec(codec);
    configuration.useSingleServer().setAddress("redis://127.0.0.1:6379");
    redisson = (Redisson) Redisson.create(configuration);
    keys = redisson.getKeys();
    keys.flushdb();
}
 
Example #16
Source File: RedissonReferenceTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseDefaultCodec() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
    JsonJacksonCodec codec = new JsonJacksonCodec(objectMapper);

    Config config = new Config();
    config.setCodec(codec);
    config.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());

    RedissonClient redissonClient = Redisson.create(config);
    RBucket<Object> b1 = redissonClient.getBucket("b1");
    b1.set(new MyObject());
    RSet<Object> s1 = redissonClient.getSet("s1");
    assertTrue(s1.add(b1));
    assertTrue(codec == b1.getCodec());

    Config config1 = new Config();
    config1.setCodec(codec);
    config1.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedissonClient redissonClient1 = Redisson.create(config1);

    RSet<RBucket> s2 = redissonClient1.getSet("s1");
    RBucket<MyObject> b2 = s2.iterator(1).next();
    assertTrue(codec == b2.getCodec());
    assertTrue(b2.get() instanceof MyObject);
    redissonClient.shutdown();
    redissonClient1.shutdown();
}
 
Example #17
Source File: RedissonTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecoderError() {
    redisson.getBucket("testbucket", new StringCodec()).set("{INVALID JSON!}");

    for (int i = 0; i < 256; i++) {
      try {
          redisson.getBucket("testbucket", new JsonJacksonCodec()).get();
          Assert.fail();
      } catch (Exception e) {
          // skip
      }
    }

    redisson.getBucket("testbucket2").set("should work");
}
 
Example #18
Source File: RedissonReferenceReactiveTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseDefaultCodec() throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
    JsonJacksonCodec codec = new JsonJacksonCodec(objectMapper);

    Config config = new Config();
    config.setCodec(codec);
    config.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());

    RedissonReactiveClient reactive = Redisson.createReactive(config);
    RBucketReactive<Object> b1 = reactive.getBucket("b1");
    sync(b1.set(new MyObject()));
    RSetReactive<Object> s1 = reactive.getSet("s1");
    assertTrue(sync(s1.add(b1)));
    assertTrue(codec == b1.getCodec());

    Config config1 = new Config();
    config1.setCodec(codec);
    config1.useSingleServer()
            .setAddress(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedissonReactiveClient reactive1 = Redisson.createReactive(config1);

    RSetReactive<RBucketReactive> s2 = reactive1.getSet("s1");
    RBucketReactive<MyObject> b2 = sync(s2.iterator(1));
    assertTrue(codec == b2.getCodec());
    assertTrue(sync(b2.get()) instanceof MyObject);
    reactive.shutdown();
    reactive1.shutdown();
}
 
Example #19
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** key是否存在(Hash) */
public boolean hIsExists(String key) {
    Object map = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中读
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map != null) {
            return map != NULL;
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = rmap.isExists();
    if (cache != null) {
        if (exists) {
            cache.put(key, new HashMap<>(16));
        } else {
            cache.put(key, NULL);
        }
    }
    return exists;
}
 
Example #20
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void set(String key, Object value) {
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        map.put(key, value);
    }
    redisson.getBucket(key, JsonJacksonCodec.INSTANCE).set(value);
}
 
Example #21
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void set(String key, Object value, long timeToLive,
                TimeUnit timeUnit) {
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        map.put(key, value);
    }
    redisson.getBucket(key, JsonJacksonCodec.INSTANCE).set(value,
            timeToLive, timeUnit);
}
 
Example #22
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 写(当无值时) */
public boolean trySet(String key, Object value, long timeToLive,
                      TimeUnit timeUnit) {
    boolean success = redisson.getBucket(key, JsonJacksonCodec.INSTANCE)
            .trySet(value, timeToLive, timeUnit);
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        if (success) {
            map.put(key, value);
        } else {
            map.remove(key);
        }
    }
    return success;
}
 
Example #23
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 删 */
public boolean remove(String key) {
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        map.put(key, NULL);
    }
    return redisson.getBucket(key, JsonJacksonCodec.INSTANCE).delete();
}
 
Example #24
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 读(Hash) */
@SuppressWarnings("unchecked")
public <T> Map<String, T> hget(String key) {
    Object map = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中读
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map != null) {
            if (map == NULL) {
                // key不存在
                return null;
            }
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = map != null || rmap.isExists();
    if (exists) {
        map = new HashMap<>(rmap);
    }
    if (cache != null) {
        cache.put(key, map == null ? NULL : map);
    }
    return (Map<String, T>) map;
}
 
Example #25
Source File: MultiCache.java    From mPass with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void hset(String key, Map<String, ?> map, long timeToLive,
                 TimeUnit timeUnit) {
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        cache.put(key, map);
    }
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    rmap.clear();
    rmap.putAll(map);
    rmap.expire(timeToLive, timeUnit);
}
 
Example #26
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void hset(String key, Map<String, ?> map, long timeToLive,
                 TimeUnit timeUnit) {
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        cache.put(key, map);
    }
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    rmap.clear();
    rmap.putAll(map);
    rmap.expire(timeToLive, timeUnit);
}
 
Example #27
Source File: MultiCache.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 写,当key不存在时无效,并返回false */
@SuppressWarnings("unchecked")
public boolean hset(String key, String field, Object value) {
    Object map = null;
    /**
     * CACHE:key==NULL 表示key不存在<br>
     * CACHE:key==MAP 表示key存在
     */
    // 从线程中判断key是否存在
    Map<String, Object> cache = getThreadLocal();
    if (cache != null) {
        map = cache.get(key);
        if (map == NULL) {
            // key不存在,写失败
            return false;
        }
    }
    // 从redis读并回写线程
    RMap<String, Object> rmap = redisson.getMap(key,
            JsonJacksonCodec.INSTANCE);
    boolean exists = map != null || rmap.isExists();
    if (!exists) {
        if (cache != null) {
            cache.put(key, NULL);
        }
        return false;
    }
    // 写入数据
    rmap.put(field, value);
    if (cache != null) {
        if (map == null) {
            map = new HashMap<>(16);
            cache.put(key, map);
        }
        ((Map<String, Object>) map).put(field,
                value == null ? NULL : value);
    }
    return true;
}
 
Example #28
Source File: MultiCache.java    From mPass with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void set(String key, Object value) {
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        map.put(key, value);
    }
    redisson.getBucket(key, JsonJacksonCodec.INSTANCE).set(value);
}
 
Example #29
Source File: MultiCache.java    From mPass with Apache License 2.0 5 votes vote down vote up
/** 写 */
public void set(String key, Object value, long timeToLive,
                TimeUnit timeUnit) {
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        map.put(key, value);
    }
    redisson.getBucket(key, JsonJacksonCodec.INSTANCE).set(value,
            timeToLive, timeUnit);
}
 
Example #30
Source File: MultiCache.java    From mPass with Apache License 2.0 5 votes vote down vote up
/** 写(当无值时) */
public boolean trySet(String key, Object value, long timeToLive,
                      TimeUnit timeUnit) {
    boolean success = redisson.getBucket(key, JsonJacksonCodec.INSTANCE)
            .trySet(value, timeToLive, timeUnit);
    Map<String, Object> map = getThreadLocal();
    if (map != null) {
        if (success) {
            map.put(key, value);
        } else {
            map.remove(key);
        }
    }
    return success;
}