Java maven项目整合Redis

本文介绍了为何在Java项目中使用Redis,强调了其高速、丰富数据类型、原子性和数据复制等特性,并详细阐述了在已有Redis安装基础上,如何通过添加依赖、配置以及创建帮助类将Redis整合进Java Maven项目。
摘要由CSDN通过智能技术生成

1、为什么要使用Redis?

Redis是一个key-value存储系统。主要用于解决分布式系统中的多台主从机之间的数据同步和共享问题。

2、Redis有哪些特点?

1)、redis的数据完全存储在内存中,使用磁盘只用于持久性,所以redis的速度非常快;

2)、相比许多键值存储系统,redis拥有较为丰富的数据类型;

3)、redis的操作都是原子性的,所以在异步的时候也是安全的;

4)、redis可以将数据复制到任意数量的从机。

3、如何将redis整合到项目中?(默认redis已经安装好了)

1)、pom文件中引入相关包

         <dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>


2)、redis配置

#config for redis
redis.pool.maxActive=512
redis.pool.maxIdle=100
redis.pool.maxWait=100000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=172.17.6.148
redis.port=6379
redis.expire=1200

3)、封装redis帮助类

public class RedisProvider {

	protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class);
	protected static JedisPool jedispool;
	protected static int EXPIRE = 130;
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
        if (bundle == null) {
            throw new IllegalArgumentException(
                    "[redis.properties] is not found!");
        }

        EXPIRE = Integer.valueOf(bundle.getString("redis.expire"));
        
        JedisPoolConfig jedisconfig = new JedisPoolConfig();
        jedisconfig.setMaxActive(Integer.valueOf(bundle
                .getString("redis.pool.maxActive")));
        jedisconfig.setMaxIdle(Integer.valueOf(bundle
                .getString("redis.pool.maxIdle")));
        jedisconfig.setMaxWait(Long.valueOf(bundle
                .getString("redis.pool.maxWait")));
        jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle
                .getString("redis.pool.testOnBorrow")));
        jedisconfig.setTestOnReturn(Boolean.valueOf(bundle
                .getString("redis.pool.testOnReturn")));
        jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),
                Integer.valueOf(bundle.getString("redis.port")), 100000);
	}
	
	public static Jedis getJed
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值