栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis客户端访问的三种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis客户端访问的三种方式

1. java程序访问Redis

使用 Jedis API进行访问

    导入jar包

	redis.clients 
	jedis 
	2.9.0 

    使用API连接
@Test
public void testConn(){ 
	//与Redis建立连接 IP+port 
	Jedis redis = new Jedis("192.168.127.128", 6379); 
	//在Redis中写字符串 key value 
	redis.set("jedis:name:1","jd-zhangfei"); 
	//获得Redis中字符串的值 
	System.out.println(redis.get("jedis:name:1")); 
	//在Redis中写list 
	redis.lpush("jedis:list:1","1","2","3","4","5"); 
	//获得list的长度 
	System.out.println(redis.llen("jedis:list:1")); 
}
2. Spring访问Redis
    添加Redis依赖
 
	org.springframework.data 
	spring-data-redis 
	1.0.3.RELEASE 

    配置Spring.xml文件
 
 
	 
		 
			
				classpath:redis.properties
			
		
	
	
	
		
		
		
		
	
	
		
		
		
		
	
	
		
		
			 
		
		
			 
		
	

    添加redis.properties
redis.pool.maxActive=100 
redis.pool.maxIdle=50 
redis.pool.maxWait=1000 
redis.pool.testOnBorrow=true 
redis.timeout=50000 
redis.server=192.168.72.128 
redis.port=6379
    java代码
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 
import java.io.Serializable; 
@ContextConfiguration({ "classpath:redis.xml" }) 
public class RedisTest extends AbstractJUnit4SpringContextTests { 
	@Autowired 
	private RedisTemplate rt; 
	
	@Test 
	public void testConn() { 
		rt.opsForValue().set("name","zhangfei"); 			
		System.out.println(rt.opsForValue().get("name"));
	} 
}
3. SpringBoot访问Redis
    添加配置文件application.yml
spring: 
	redis: 
		host: 192.168.72.128 
		port: 6379 
		jedis: 
			pool: 
				min-idle: 0 
				max-idle: 8 
				max-active: 80 
				max-wait: 30000 
				timeout: 3000
    添加配置类RedisConfig
package com.lagou.sbr.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory factory;

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}
    测试一下
//注入RedisTemplate即可,就可以使用了
@Autowired 
RedisTemplate redisTemplate;

redisTemplate.opsForValue().set(key,value,20, TimeUnit.SECONDS);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749844.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号