Search in sources :

Example 41 with CacheConfigBean

use of info.xiancloud.core.support.cache.CacheConfigBean in project xian by happyyangyuan.

the class CacheDecrementUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Long value = msg.get("value", Long.class);
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    long decrement = 0;
    try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
        if (value != null)
            decrement = ObjectCacheOperate.decrBy(jedis, key, value);
        else
            decrement = ObjectCacheOperate.decr(jedis, key);
        if (msg.getArgMap().containsKey("timeout")) {
            int timeout = CacheOperateManager.correctionTimeout(msg.get("timeout", int.class));
            if (timeout > -1)
                ObjectCacheOperate.expire(jedis, key, timeout);
        }
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
    return UnitResponse.success(decrement);
}
Also used : Jedis(redis.clients.jedis.Jedis) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 42 with CacheConfigBean

use of info.xiancloud.core.support.cache.CacheConfigBean in project xian by happyyangyuan.

the class CacheIncrementUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Long value = msg.get("value", Long.class);
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    long increment = 0;
    try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
        if (value != null)
            increment = ObjectCacheOperate.incrBy(jedis, key, value);
        else
            increment = ObjectCacheOperate.incr(jedis, key);
        if (msg.getArgMap().containsKey("timeout")) {
            int timeout = CacheOperateManager.correctionTimeout(msg.get("timeout", int.class));
            if (timeout > -1)
                ObjectCacheOperate.expire(jedis, key, timeout);
        }
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
    return UnitResponse.success(increment);
}
Also used : Jedis(redis.clients.jedis.Jedis) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 43 with CacheConfigBean

use of info.xiancloud.core.support.cache.CacheConfigBean in project xian by happyyangyuan.

the class CacheListAddAllUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.get("key", String.class);
    List values = msg.get("values", List.class);
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
        if (values != null && !values.isEmpty()) {
            String info = ServerOperate.info(jedis, "server");
            String redis_mode = ServerOperate.getAttributeInInfo(info, "redis_mode");
            if (redis_mode != null && "standalone".equals(redis_mode)) {
                Pipeline pipeline = jedis.pipelined();
                values.stream().forEach(valueObj -> {
                    String value = FormatUtil.formatValue(valueObj);
                    pipeline.rpush(key, value);
                });
                pipeline.sync();
            } else {
                values.stream().forEach(valueObj -> {
                    String value = FormatUtil.formatValue(valueObj);
                    jedis.rpush(key, value);
                });
            }
        }
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
    return UnitResponse.success();
}
Also used : Jedis(redis.clients.jedis.Jedis) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) List(java.util.List) Pipeline(redis.clients.jedis.Pipeline)

Example 44 with CacheConfigBean

use of info.xiancloud.core.support.cache.CacheConfigBean in project xian by happyyangyuan.

the class CacheListAddUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Object valueObj = msg.getArgMap().get("valueObj");
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        long length = Redis.call(cacheConfigBean, (jedis) -> {
            String value = FormatUtil.formatValue(valueObj);
            return jedis.rpush(key, value);
        });
        return UnitResponse.success(length);
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
}
Also used : CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 45 with CacheConfigBean

use of info.xiancloud.core.support.cache.CacheConfigBean in project xian by happyyangyuan.

the class CacheListExistsUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        boolean result = Redis.call(cacheConfigBean, jedis -> jedis.exists(key));
        return UnitResponse.success(result);
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
}
Also used : CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Aggregations

CacheConfigBean (info.xiancloud.core.support.cache.CacheConfigBean)46 Jedis (redis.clients.jedis.Jedis)15 Redis (info.xiancloud.cache.redis.Redis)4 CacheGroup (info.xiancloud.cache.service.CacheGroup)4 Group (info.xiancloud.core.Group)4 Input (info.xiancloud.core.Input)4 Unit (info.xiancloud.core.Unit)4 UnitMeta (info.xiancloud.core.UnitMeta)4 UnitRequest (info.xiancloud.core.message.UnitRequest)4 UnitResponse (info.xiancloud.core.message.UnitResponse)4 Set (java.util.Set)3 JSONObject (com.alibaba.fastjson.JSONObject)2 List (java.util.List)2 Map (java.util.Map)2 FormatUtil (info.xiancloud.cache.redis.util.FormatUtil)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Pipeline (redis.clients.jedis.Pipeline)1 ScanParams (redis.clients.jedis.ScanParams)1