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

cn.wizzer.app.cms.commons.ig.RedisIdGenerator Maven / Gradle / Ivy

There is a newer version: 5.2.8-zookeeper
Show newest version
package cn.wizzer.app.cms.commons.ig;

import org.nutz.integration.jedis.JedisAgent;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.List;

/**
 * Created by wizzer on 2018/3/17.
 */

@IocBean
public class RedisIdGenerator implements IdGenerator {

    @Inject
    protected JedisAgent jedisAgent;

    public RedisIdGenerator() {
    }

    public RedisIdGenerator(JedisAgent jedisAgent) {
        this.jedisAgent = jedisAgent;
    }

    public String next(String tableName, String prefix) {
        String key = prefix.toUpperCase();
        if (key.length() > 16) {
            key = key.substring(0, 16);
        }
        try (Jedis jedis = jedisAgent.getResource()) {
            String ym = Times.format("yyyyMM",new Date());
            String id = String.valueOf(jedis.incr("nutzwk:ig:" + tableName.toUpperCase() + ym));
            return key + ym + Strings.alignRight(id, 10, '0');
        }
    }

    public Object run(List fetchParam) {
        return next((String) fetchParam.get(0), (String) fetchParam.get(1));
    }

    public String fetchSelf() {
        return "ig";
    }

}