Redis配置文件配置

Posted Cleverlover

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis配置文件配置相关的知识,希望对你有一定的参考价值。

Spring和Redis整合:

 配置applicationContext-redis.xml,添加Redis服务:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
    <!-- 包扫描器 -->
    <context:annotation-config/>
    <!-- redis单机版 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.121.133"/>
        <constructor-arg name="port" value="6379"/>
    </bean>
    <bean id="jedisClientPool" class="net.wanho.jedis.JedisClientPool"/>

</beans>

要写一个工具类:

package net.wanho.jedis;

public interface JedisClient {

    String set(String key, String value);
    String get(String key);
    Boolean exists(String key);
    Long expire(String key, int seconds);
    Long ttl(String key);
    Long incr(String key);
    Long hset(String key, String field, String value);
    String hget(String key, String field);
    Long hdel(String key, String... field);
    void delete(String key);
}
package net.wanho.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

public class JedisClientPool implements JedisClient {
    
    @Resource
    private JedisPool jedisPool;

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.set(key, value);
        jedis.close();
        return result;
    }

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    @Override
    public Boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        Boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    @Override
    public Long expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, seconds);
        jedis.close();
        return result;
    }

    @Override
    public Long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public Long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public Long hset(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return result;
    }

    @Override
    public String hget(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.hget(key, field);
        jedis.close();
        return result;
    }

    @Override
    public Long hdel(String key, String... field) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(key, field);
        jedis.close();
        return result;
    }

    @Override
    public void delete(String key) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(key);
    }

}

暴露服务,配置applicationContext-service.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 配置扫描,自动加载service类 -->
    <context:component-scan base-package="net.wanho.service" />

    <!-- 发布dubbo服务 -->
    <dubbo:application name="wl-service"/>
    <!-- 提供依赖信息 -->
    <dubbo:registry protocol="zookeeper" address="192.168.121.133:2181" />
    <!-- 暴露一个服务在20880端口 -->
    <dubbo:protocol name="dubbo" port="20881"/>
    <!-- 暴露一个实际服务 -->
    <dubbo:service interface="net.wanho.service.UserService" ref="userServiceImpl" timeout="30000"/>
</beans>

 

以上是关于Redis配置文件配置的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段11——vue路由的配置

VS Code配置markdown代码片段

VS Code配置markdown代码片段

VS Code配置snippets代码片段快速生成html模板,提高前端编写效率

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

如何为 XSLT 代码片段配置 CruiseControl 的 C# 版本?