扩展 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
    评论
JFinal实现Flowable审批流程的方法可以通过以下步骤实现: 1. 在流程表单数据正式提交审核前,需要将流程表单数据formData及流程执行实例ExecutionEntity传递给接口。 2. 首先,需要引入Flowable依赖并配置好相关的数据库连接信息。 3. 在JFinal创建一个继承自FlowablePlugin插件类,并进行相关配置,例如设置数据库连接、流程引擎配置等。 4. 在JFinal的配置文件添加FlowablePlugin插件配置。 5. 创建一个继承自FlowableController的控制器类,并实现具体的审批逻辑。 6. 在控制器类,可以使用Flowable提供的API方法来创建流程、启动流程实例、查询任务列表、完成任务等操作。 7. 对于多级审批的场景,可以使用多实例方式或者流程绘制多个审批人的方式来实现。使用多实例方式可以通过设置流程变量来指定审批人的信息,实现逐级审批。 8. 在流程,可以使用条件判断来实现不同分支的审批逻辑。例如,使用if语句判断流程是否是一个ExclusiveGateway对象,根据判断结果执行不同的逻辑。 综上所述,以上是在JFinal实现Flowable审批流程的一种方法。可以根据具体的需求和业务逻辑进行相应的调整和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Flowable 实现【选择下一步流程审核人】](https://blog.csdn.net/weixin_39805539/article/details/111519749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [flowable 实现逐级审批功能](https://blog.csdn.net/qq_17798399/article/details/123424189)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值