博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis和Spring整合
阅读量:4954 次
发布时间:2019-06-12

本文共 10999 字,大约阅读时间需要 36 分钟。

Redis和Spring整合

Redis在这篇里就不做介绍了~以后系统的学学,然后整理写出来。

首先是环境的搭建

通过自己引包的方式,将redis和spring-redis的包引到自己的项目中,我项目中是通过pom引进的,如下(貌似版本都是写着篇博客时最新的):

org.springframework.data
spring-data-redis
1.8.1.RELEASE
redis.clients
jedis
2.9.0
jar
compile

通过pom的依赖可以看出来,我用的Jedis作为我的Cilent。

配置Spring的配置文件

现在spring-boot框架大火,但是目前,我还是为了项目的可读性,使用配置文件的方式进行配置。以后有机会学习一下注解的方式,在总结出来。

classpath:init.properties

大家可以看见,我这个配置文件写的比较奇葩...

因为我一共写了两个pool的bean,由于目前项目redis还是单机版本,所以我只是使用JedisPool这个进行之后的操作。ShardedJedisPool这个适用于分布式redis缓存的连接,但是我有一天突然发现没有flushDB的方法,我一着急,就换做了JedisPool,以后加强学习,把更加适用的方法总结出来。

RedisDataSource

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;@Repository("jedisDS")public class JedisDataSourceImpl implements JedisDataSource{    private static final Logger LOGGER = LoggerFactory.getLogger(JedisDataSourceImpl.class);    @Autowired    private ShardedJedisPool shardedJedisPool;    @Autowired    private JedisPool jedisPool;    @Override    public ShardedJedis getShardedRedisClient() {        ShardedJedis shardedJedis = null;        try{            shardedJedis = shardedJedisPool.getResource();            //shardedJedisPool.            return shardedJedis;        }catch (Exception e){            LOGGER.error("[JedisDS] getRedisClient error: "+e.getMessage());            if(null!=shardedJedis){                shardedJedis.close();            }            return null;        }    }    @Override    public Jedis getRedisClient() {        Jedis jedis = null;        try{            jedis = jedisPool.getResource();            return jedis;        }catch (Exception e){            if(null!=jedis){                jedis.close();            }            LOGGER.error("[JedisDS] getJedisClient error: "+e.getMessage());        }        return null;    }    @Override    public void returnResource(ShardedJedis shardedJedis) {        shardedJedis.close();    }    @Override    public void returnResource(ShardedJedis shardedJedis, boolean broken) {        shardedJedis.close();    }    @Override    public void returnResource(Jedis jedis, boolean broken) {        jedis.close();    }    }

上面的代码写了两种JedisPool和ShardedJedisPool两种方式

RedisCilentTamplate

这里整理了一些简单的方法,可以直接调用,可以直接参考一下

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import redis.clients.jedis.Jedis;import redis.clients.jedis.ShardedJedis;@Repository("redisClientTemplate")public class RedisClientTemplate {    private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class);    @Autowired    private JedisDataSource redisDataSource;    public void disconnect() {        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        shardedJedis.disconnect();    }    /**     * 设置单个值     *     * @param key     * @param value     * @return     */    public String set(String key, String value) {        String result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.set(key, value);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    /**     * 获取单个值     *     * @param key     * @return     */    public String get(String key) {        String result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.get(key);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public Boolean exists(String key) {        Boolean result = false;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.exists(key);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public String type(String key) {        String result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.type(key);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    /**     * 在某段时间后失效     *     * @param key     * @param seconds     * @return     */    public Long expire(String key, int seconds) {        Long result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.expire(key, seconds);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    /**     * 在某个时间点失效     *     * @param key     * @param unixTime     * @return     */    public Long expireAt(String key, long unixTime) {        Long result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.expireAt(key, unixTime);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public Long ttl(String key) {        Long result = null;        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.ttl(key);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public boolean setbit(String key, long offset, boolean value) {        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        boolean result = false;        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.setbit(key, offset, value);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public boolean getbit(String key, long offset) {        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        boolean result = false;        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.getbit(key, offset);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public long setRange(String key, long offset, String value) {        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        long result = 0;        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.setrange(key, offset, value);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public String getRange(String key, long startOffset, long endOffset) {        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        String result = null;        if (shardedJedis == null) {            return result;        }        boolean broken = false;        try {            result = shardedJedis.getrange(key, startOffset, endOffset);        } catch (Exception e) {            log.error(e.getMessage(), e);            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);        }        return result;    }    public boolean del(String key){        ShardedJedis shardedJedis = redisDataSource.getShardedRedisClient();        boolean temp = false;        if (shardedJedis == null) {            return false;        }        boolean broken = false;        try {            shardedJedis.del(key);            temp = true;        } catch (Exception e) {            log.error(e.getMessage(), e);            temp = false;            broken = true;        } finally {            redisDataSource.returnResource(shardedJedis, broken);            return temp;        }    }    public boolean fushAll(){        boolean temp = false;        Jedis jedis = redisDataSource.getRedisClient();        boolean broken = false;        try{            jedis.flushDB();            return true;        }catch (Exception e){            log.error(e.getMessage(), e);            temp = false;            broken = true;        }finally {            redisDataSource.returnResource(jedis, broken);            return temp;        }    }}

转载于:https://www.cnblogs.com/luanpeng/p/6651612.html

你可能感兴趣的文章
深拷贝 vs 浅拷贝 释放多次
查看>>
关于android系统不关屏设置
查看>>
SONY VPCS138EC降级安装XP
查看>>
[luogu4201][bzoj1063]设计路线【树形DP】
查看>>
手机抓包-手机劫持域名到指定服务器
查看>>
被放逐的皇后 金建云
查看>>
Javascript 有用参考函数
查看>>
点群的判别(三)
查看>>
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>