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

nbcp.myoql.db.redis.MyRedisKeySerializerWithProductLine.kt Maven / Gradle / Ivy

The newest version!
package nbcp.myoql.db.redis

import nbcp.base.comm.config
import nbcp.base.extend.HasValue
import nbcp.base.extend.scopes
import org.springframework.data.redis.serializer.StringRedisSerializer

/**
 * 带产品线前缀的 Redis Key
 */
class MyRedisKeySerializerWithProductLine : StringRedisSerializer() {
    override fun serialize(key: String): ByteArray {
        var key2 = key;
        val prefix = getProductLineCodePrefix();

        if (prefix.HasValue && !key2.startsWith(prefix + ":")) {
            key2 = prefix + ":" + key2;
        }
        return super.serialize(key2)
    }

    override fun deserialize(bytes: ByteArray?): String {
        var key2 = super.deserialize(bytes)
        val prefix = getProductLineCodePrefix();

        if (prefix.HasValue && key2.startsWith(prefix + ":")) {
            key2 = key2.substring(prefix.length + 1);
        }
        return key2;
    }

    private fun getProductLineCodePrefix(): String {
        var productLineCodeScope = scopes.getLatest(RedisProductLineScope::class.java)
        if (productLineCodeScope != null) {
            return productLineCodeScope.productLineCode;
        }

        if (config.redisProductLineCodePrefixEnable) {
            return config.appPrefix;
        }

        return "";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy