All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.redis.item.StringIntegerRedisCacheItem Maven / Gradle / Ivy

There is a newer version: 2023.1.17
Show newest version
package shz.redis.item;

import shz.core.NullHelp;
import shz.core.NumberHelp;
import shz.core.ToList;
import shz.redis.RedisCacheItem;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public interface StringIntegerRedisCacheItem extends RedisCacheItem {
    @Override
    default Operator setKey(Object... keys) {
        return new Operator(this, name(), keys);
    }

    class Operator extends RedisCacheItem.Operator {
        protected Operator(StringIntegerRedisCacheItem cacheItem, String name, Object... keys) {
            super(cacheItem, name, keys);
        }

        public final Integer get() {
            return NumberHelp.getInteger(cacheItem.getCache().get(key));
        }

        @SafeVarargs
        public final  List get(Collection collection, Function... keyGetters) {
            List objects = cacheItem.getCache().multiGet(concatKeys(collection, keyGetters));
            if (NullHelp.isEmpty(objects)) return Collections.emptyList();
            return ToList.explicitCollect(objects.stream().map(NumberHelp::getInteger), objects.size());
        }

        public final Integer get(Supplier supplier) {
            return NumberHelp.getInteger(cacheItem.getCache().get(key, supplier, cacheItem.getExpireMillis(), cacheItem.getNullKeyExpireMillis(), cacheItem.getTimeoutMillis()));
        }

        public final void set(Integer val) {
            cacheItem.getCache().set(key, val, cacheItem.getExpireMillis(), cacheItem.getNullKeyExpireMillis());
        }

        public final boolean setNx(Integer val, long expireMillis) {
            return cacheItem.getCache().setNx(key, val, expireMillis);
        }

        public final boolean setNx(Integer val) {
            return setNx(val, cacheItem.getExpireMillis());
        }

        public final boolean setXx(Integer val, long expireMillis) {
            return cacheItem.getCache().setXx(key, val, expireMillis);
        }

        public final boolean setXx(Integer val) {
            return setXx(val, cacheItem.getExpireMillis());
        }

        public final Integer increment() {
            Long result = cacheItem.getCache().increment(key);
            return result == null ? null : result.intValue();
        }

        public final Integer increment(int delta) {
            Long result = cacheItem.getCache().increment(key, delta);
            return result == null ? null : result.intValue();
        }

        public final Integer decrement() {
            Long result = cacheItem.getCache().decrement(key);
            return result == null ? null : result.intValue();
        }

        public final Integer decrement(int delta) {
            Long result = cacheItem.getCache().decrement(key, delta);
            return result == null ? null : result.intValue();
        }
    }
}