redis可以说是现在最火的非关系型数据库,主要是它处理数据的能力是真的很强。就说win环境的处理能力一般的机器也能在每秒3万次以上,已经很厉害了。我们一般的几万用户的APP根本不需要集群,一个Redis即可搞定几乎所有的小规模并发性问题了。
资源地址:redis服务(windows版)&redis可视化工具.rar_asp.netcoreredis-.Net文档类资源-CSDN下载
目录
2、配置声明(application.properties中)
6、创建测试接口【com.item.controller】内
8、启动测试http://127.0.0.1:8088/swagger-ui.htm
<!-- Redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
#---------------------------------
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
#---------------------------------
中间有输出语句就是为了表现配置成功,可以删掉。
package com.item.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* 完成对Redis整合的配置
*/
@Configuration
public class RedisConfig {
/**
* 1.创建 JedisPoolConfig 对象。在该对象中完成一些链接池配置
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public JedisPoolConfig JedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
System.out.println("redis默认值:" + config.getMaxIdle());
System.out.println("redis默认值:" + config.getMinIdle());
System.out.println("redis默认值:" + config.getMaxTotal());
return config;
}
/**
* 2.创建 JedisConnectionFactory 对象,配置Redis连接属性
*
* @param config
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
System.out.println("redis配置完毕:" + config.getMaxIdle());
System.out.println("redis配置完毕:" + config.getMinIdle());
System.out.println("redis配置完毕:" + config.getMaxTotal());
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setPoolConfig(config);//关联连接池的配置对象
return factory;
}
/**
* 3.创建RedisTemplate,用于执行Redis操作的方法
*
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联JedisConnectionFactory
template.setConnectionFactory(factory);
//为key序列化器
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化器
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
//通过依赖注入使用redis
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
package com.item.redis;
import com.item.Base.RedisUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
public class RedisBase {
/**
* 返回的是Object
*/
@Autowired
private RedisTemplate redisTemplate;
/**
* 返回String
*/
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 添加值
* @param key
* @param value
* @return
*/
public boolean redisSet(String key,String value){
key= RedisUrl.url+key;
stringRedisTemplate.opsForValue().set(key,value);
String o = stringRedisTemplate.opsForValue().get(key);
return o==null?false:true;
}
/**
* 存储带倒计时的字符串
* @param key
* @param value
* @param second
* @return
*/
public boolean redisSetTime(String key,String value,long second){
key= RedisUrl.url+key;
stringRedisTemplate.opsForValue().set(key,value,second);
String s = stringRedisTemplate.opsForValue().get(key);
return s==null?false:true;
}
/**
* 获取key的value
* @param key
* @return
*/
public String redisGet(String key){
key= RedisUrl.url+key;
return stringRedisTemplate.opsForValue().get(key);
}
}
package com.item.controller;
import com.item.model.Users;
import com.item.redis.RedisBase;
import com.item.res.SUCCESS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("用户操作接口")
@RestController
@CrossOrigin
@RequestMapping("Redis/")
public class RedisController extends RedisBase {
/**
* 测试Redis添加
*/
@GetMapping("SetRedis")
@ApiOperation(value = "添加key_value",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "key",required = true,paramType = "query",dataType = "String",value = "redis_key"),
@ApiImplicitParam(name = "value",required = true,paramType = "query",dataType = "String",value = "redis_value")
})
public Object testRedisSet(String key,String value){
redisSet(key,value);
return new SUCCESS("写入成功");
}
/**
* 测试Redis查询
*/
@GetMapping("GetRedis")
@ApiOperation(value = "获取key信息",notes = "没啥留言的")
@ApiImplicitParams({
@ApiImplicitParam(name = "key",required = true,paramType = "query",dataType = "String",value = "redis_key")
})
public Object testRedisGet(String key){
return new SUCCESS(redisGet(key));
}
}
package com.item.Base;
public class RedisUrl {
public static final String url="demo:infos:";
}
能写入,能查询,没问题。
我个人喜欢用String和list,自己用自己封装吧。