扩展 JFinal 中的 Redis 极速化插件 — RedisPlugin

RedisPlugin 是 JFinal 支持 Redis 的极速化插件。使用 RedisPlugin 可以极度方便的使用 redis,该插件不仅提供了丰富的 API,而且还同时支持多 redis 服务端。Redis 拥有超高的性能,丰富的数据结构,天然支持数据持久化,是目前应用非常广泛的 nosql 数据库。对于 redis 的有效应用可极大提升系统性能,节省硬件成本。RedisPlugin 详情可见:https://jfinal.com/doc

常规用法

1、Maven依赖

<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal</artifactId>
    <version>4.8</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>de.ruedigermoeller</groupId>
    <artifactId>fst</artifactId>
    <version>2.43</version>
</dependency>

2、初始化

private void initRedis() {
    String crawlerIp = PropKit.get("redis.crawler.ip");
    Integer crawlerPort = PropKit.getInt("redis.crawler.port");
    String crawlerAuth = PropKit.get("redis.crawler.auth");
    Integer crawlerTimeout = PropKit.getInt("redis.crawler.timeout", 30000);
    Integer crawlerDatabase = PropKit.getInt("redis.crawler.database", 0);
    RedisPlugin crawlerPlugin = new RedisPlugin("CRAWLER",
            crawlerIp, crawlerPort, crawlerTimeout, crawlerAuth, crawlerDatabase);
    crawlerPlugin.start();
}

3、使用DEMO

Redis.use().XXXXXX();

其中,“XXXXXX()”代表具体数据结构相对应的增删改查等操作方法,操作方法同:https://www.runoob.com/redis/redis-tutorial.html

问题描述

Jedis 的三方库提供的 zrangeByScore() 可返回有序集成员 score 值介于 min 和 max 之间(包括等于 min 或 max)且按 score 值递增(从小到大)次序排列的 offset 起的 count 个成员。相关代码如下:

  public Set<byte[]> zrangeByScore(final byte[] key, final double min, final double max,
      final int offset, final int count) {
    return zrangeByScore(key, toByteArray(min), toByteArray(max), offset, count);
  }

  public Set<byte[]> zrangeByScore(final byte[] key, final byte[] min, final byte[] max,
      final int offset, final int count) {
    checkIsInMultiOrPipeline();
    client.zrangeByScore(key, min, max, offset, count);
    return SetFromList.of(client.getBinaryMultiBulkReply());
  }

而 JFinal 提供的 Cache 对象(Cache 对象提供了丰富的 API 用于使用 Redis 服务)的 zrangeByScore() 仅返回有序集成员 score 值介于 min 和 max 之间的所有成员。相关代码如下:

    public Set zrangeByScore(Object key, double min, double max) {
        Jedis jedis = this.getJedis();

        LinkedHashSet var9;
        try {
            Set<byte[]> data = jedis.zrangeByScore(this.keyToBytes(key), min, max);
            Set<Object> result = new LinkedHashSet();
            this.valueSetFromBytesSet(data, result);
            var9 = result;
        } finally {
            this.close(jedis);
        }

        return var9;
    }

解决方案

扩展 JFinal 中的 Cache 对象。之所以选择扩展 JFinal 中的 Cache 对象,而非直接使用 Jedis 的三方库,是因为 JFinal 中的 Cache 对象封装了一套完整的 Redis 管理体系,管理着 Redis 的连接,及 Redis Key 的命名策略。Cache 对象的初始化相关代码如下:

    public boolean start() {
        JedisPool jedisPool;
        if (this.port != null && this.timeout != null && this.database != null && this.clientName != null) {
            jedisPool = new JedisPool(this.jedisPoolConfig, this.host, this.port.intValue(), this.timeout.intValue(), this.password, this.database.intValue(), this.clientName);
        } else if (this.port != null && this.timeout != null && this.database != null) {
            jedisPool = new JedisPool(this.jedisPoolConfig, this.host, this.port.intValue(), this.timeout.intValue(), this.password, this.database.intValue());
        } else if (this.port != null && this.timeout != null) {
            jedisPool = new JedisPool(this.jedisPoolConfig, this.host, this.port.intValue(), this.timeout.intValue(), this.password);
        } else if (this.port != null) {
            jedisPool = new JedisPool(this.jedisPoolConfig, this.host, this.port.intValue());
        } else {
            jedisPool = new JedisPool(this.jedisPoolConfig, this.host);
        }

        if (this.serializer == null) {
            this.serializer = FstSerializer.me;
        }

        if (this.keyNamingPolicy == null) {
            this.keyNamingPolicy = IKeyNamingPolicy.defaultKeyNamingPolicy;
        }

        Cache cache = new Cache(this.cacheName, jedisPool, this.serializer, this.keyNamingPolicy);
        Redis.addCache(cache);
        return true;
    }

其中,JedisPool 维护着 Redis 的连接,serializer 和 keyNamingPolicy 维护着 Redis Key 的命名策略。Redis Key 命名策略的相关代码如下:

    protected byte[] keyToBytes(Object key) {
        String keyStr = this.keyNamingPolicy.getKeyName(key);
        return this.serializer.keyToBytes(keyStr);
    }

拓展DEMO

public class CaptainCache extends Cache {

    private Cache cache;

    public CaptainCache(Cache cache) {
        this.cache = cache;
        name = cache.getName();
        serializer = cache.getSerializer();
        keyNamingPolicy = cache.getKeyNamingPolicy();
    }

    /**
     * 返回有序集 key 中,所有 score 值介于 min 和 max 之间(包括等于 min 或 max )的成员。
     * 有序集成员按 score 值递增(从小到大)次序排列。
     */
    public Set zrangeByScore(Object key, double min, double max, int offset, int count) {
        Jedis jedis = cache.getJedis();
        try {
            Set<byte[]> data = jedis.zrangeByScore(keyToBytes(key), min, max, offset, count);
            Set<Object> result = new LinkedHashSet<>();
            valueSetFromBytesSet(data, result);
            return result;
        } finally {
            close(jedis);
        }
    }

}

其中,继续沿用 Cache 对象 Redis 的管理体系,并对 Cache 对象不支持的方法进行补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值