Search in sources :

Example 21 with CacheConfigBean

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

the class CacheListGetByIndexUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Long index = msg.getArgMap().get("index") != null ? Long.parseLong(msg.getArgMap().get("index").toString()) : 0;
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    String element = null;
    try {
        element = Redis.call(cacheConfigBean, (jedis) -> jedis.lindex(key, index));
        if (element != null && element.toString().equals("nil"))
            element = null;
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
    return UnitResponse.success(element);
}
Also used : Group(info.xiancloud.core.Group) Unit(info.xiancloud.core.Unit) Input(info.xiancloud.core.Input) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) Redis(info.xiancloud.cache.redis.Redis) UnitRequest(info.xiancloud.core.message.UnitRequest) CacheGroup(info.xiancloud.cache.service.CacheGroup) UnitResponse(info.xiancloud.core.message.UnitResponse) UnitMeta(info.xiancloud.core.UnitMeta) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean)

Example 22 with CacheConfigBean

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

the class CacheMapPutAllUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.getArgMap().get("key").toString();
    Map maps = msg.get("maps", Map.class);
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try (Jedis jedis = Redis.useDataSource(cacheConfigBean).getResource()) {
        if (maps != null && !maps.isEmpty()) {
            Map<String, String> _maps = new HashMap<>();
            maps.forEach((field, valueObj) -> {
                String value = FormatUtil.formatValue(valueObj);
                _maps.put(field.toString(), value);
            });
            jedis.hmset(key, _maps);
        }
        return UnitResponse.success();
    } catch (Exception e) {
        return UnitResponse.exception(e);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) CacheConfigBean(info.xiancloud.core.support.cache.CacheConfigBean) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with CacheConfigBean

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

the class CacheExistsUnit 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)

Example 24 with CacheConfigBean

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

the class CacheSortedSetLengthUnit method execute.

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

Example 25 with CacheConfigBean

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

the class CacheSortedSetRemoveUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String key = msg.get("key", String.class);
    Object member = msg.get("member", Object.class);
    Set members = (Set) msg.getArgMap().get("members");
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    try {
        Long result = Redis.call(cacheConfigBean, jedis -> {
            if (members != null && !members.isEmpty()) {
                String[] _members = new String[members.size()];
                Iterator<Object> iterator = members.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    _members[i] = FormatUtil.formatValue(iterator.next());
                    i++;
                }
                return jedis.srem(key, _members);
            } else if (member != null && !"".equals(member)) {
                return jedis.srem(key, FormatUtil.formatValue(member));
            } else {
                return -1L;
            }
        });
        return UnitResponse.success(result);
    } catch (Throwable e) {
        return UnitResponse.exception(e);
    }
}
Also used : Set(java.util.Set) 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