Search in sources :

Example 11 with CacheConfigBean

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

the class CacheMapGetUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    String field = msg.getArgMap().get("field").toString();
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        String element = Redis.call(cacheConfigBean, (jedis) -> jedis.hget(key, field));
        if (element != null && element.equals("nil"))
            element = null;
        return UnitResponse.success(element);
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
}
Also used : CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 12 with CacheConfigBean

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

the class CacheKeysUnit method execute.

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

Example 13 with CacheConfigBean

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

the class CacheScanUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String pattern = msg.getArgMap().get("pattern").toString();
    int count = msg.get("count", Integer.class, THRESHOLD_VALUE);
    String cursor = msg.getArgMap().get("cursor").toString();
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        JSONObject jsonObject = Redis.call(cacheConfigBean, jedis -> {
            JSONObject _jsonObject = new JSONObject();
            ScanParams params = new ScanParams().match(pattern).count(count);
            ScanResult<String> scans = jedis.scan(cursor, params);
            // 如果服务器向客户端返回 0 的游标时则表示迭代结束
            _jsonObject.put("cursor", scans.getStringCursor());
            _jsonObject.put("result", scans.getResult());
            return _jsonObject;
        });
        return UnitResponse.success(jsonObject);
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
}
Also used : CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) JSONObject(com.alibaba.fastjson.JSONObject) ScanParams(redis.clients.jedis.ScanParams)

Example 14 with CacheConfigBean

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

the class CacheSetUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Object value = msg.getArgMap().get("value");
    int ex = msg.get("ex", int.class, -1);
    long px = msg.get("px", int.class, -2);
    String nxXx = msg.get("nxXx", String.class, null);
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    String result;
    try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
        if (px > -2)
            result = ObjectCacheOperate.set(jedis, key, value, "PX", px, nxXx);
        else
            result = ObjectCacheOperate.set(jedis, key, value, "EX", ex, nxXx);
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
    if ("OK".equals(result))
        return UnitResponse.success();
    else
        return UnitResponse.failure(result, result);
}
Also used : Jedis(redis.clients.jedis.Jedis) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 15 with CacheConfigBean

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

the class CacheTTLUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        long ttl = Redis.call(cacheConfigBean, jedis -> jedis.ttl(key));
        return UnitResponse.success(ttl);
    } 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