Java中使用jedis操作redis
jedis封装了redis原有的操作命令,使用起来很简单。
只需要添加依赖即可操作:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency>
Java中的代码:
Jedis jedis = new Jedis(LOCALHOST,PORT);//设置地址和端口 jedis.auth(PASSWORD);//如果redis服务器配置了需要密码,此处必须设置 //存储集合到redis,并取出 jedis.lpush("mylist","admin","tom","jack"); System.out.print(jedis.lrange("mylist",0,-1));
客户端插入后,获取后打印输出
redis服务器端查看:
使用流水线Pipeline模式测试向redis服务器每秒可写多少数据
/** * 测试redis每秒可写入数据 */ @Test public void test01() { //参数1:主机地址,参数2:端口号 Jedis jedis = new Jedis(LOCALHOST,PORT);//连接redis //如果服务器的配置文件设置了需要密码验证 jedis.auth(PASSWORD);//输入密码验证 //开启流水线 Pipeline pipeline = jedis.pipelined(); int i = 0; try { long start = System.currentTimeMillis(); while (true) { long end = System.currentTimeMillis(); //当大于等于1秒时,结束操作 if(end-start>=1000){ break; } i++; pipeline.set("test"+i,i+"");//使用流水线方式向redis数据库插入数据 //jedis.set("test"+i,i+"");//向redis数据库插入数据 } }finally { jedis.close();//关闭连接 } //打印1秒内对redis操作的次数 System.out.println("redis每秒操作"+i+"次"); }
可以看到,每秒可写142611次,redis服务器查看,数据插入成功。
下面在springMVC中整合redis,使用redisUtils工具类操作redis更简单
第一步:添加依赖:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency>
第二步:添加属性文件:redis.properties
redis.host = xxx redis.port = 6379 redis.password = xxx redis.timeout = 5000 redis.maxTotal = 100 redis.maxIdle = 20 redis.minIdle = 5
第三步:添加xml配置文件 spring-redis.xml(在springmvc中集成的)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--加载属性文件--> <context:property-placeholder location="classpath:redis.properties"/> <!--Spring整合配置,连接池配置--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="minIdle" value="${redis.minIdle}"/> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="poolConfig"/> <constructor-arg name="host" value="${redis.host}"/> <constructor-arg name="port" value="${redis.port}"/> <constructor-arg name="timeout" value="${redis.timeout}"/> <constructor-arg name="password" value="${redis.password}"/> </bean> <bean class="com.supin51.redis.test.SpringBeanHolder"/> </beans>
第四步:添加SpringBeanHolder类加载bean
import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @Author:ShaoJiang * @description: * @Date: created in 下午10:49 2019/2/12 * @Modified By: */ public class SpringBeanHolder implements ApplicationContextAware { private static ApplicationContext ac; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ac = applicationContext; } public static Object getBean(String beanName) { return ac.getBean(beanName); } public static <T> T getBean(Class<T> clazz) { return ac.getBean(clazz); } }
第五步:添加redisUtils工具类:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * @Author:ShaoJiang * @description: * @Date: created in 下午10:48 2019/2/12 * @Modified By: */ public class RedisUtils { private static JedisPool jedisPool = null; private static ApplicationContext context = null; static { //加载xml文件 context = new ClassPathXmlApplicationContext("classpath:spring-redis.xml"); //获取jedis连接池 jedisPool = (JedisPool)SpringBeanHolder.getBean("jedisPool"); } public static void set(String key ,String value) { // Jedis jedis = null; try { if(jedis==null){ jedis = jedisPool.getResource(); jedis.set(key,value); } } catch (Exception e) { e.printStackTrace(); }finally { jedis.close(); } } public static String get(String key) { Jedis jedis = null; try { if(jedis==null){ jedis = jedisPool.getResource(); return jedis.get(key); } } catch (Exception e) { e.printStackTrace(); }finally { jedis.close(); } return null; } }
最后测试:
/** * 使用redisUtils工具类操作redis */ @Test public void test03() { RedisUtils.set("name","tom"); System.out.println(RedisUtils.get("name")); }
客户端测试类,插入数据后,获取输出。
redis服务器查看: