文章目录
1、配置redis的环境
在虚拟机或者服务器上,安装redis之后,配置redis的启动文件
# 需要修改以下内容
# 允许远程访问
bind 0.0.0.0
# redis在后台运行
daemonize yes
# 为了安全还是设置个密码吧
requirepass root
配置完成之后启动 redis 服务 (配置文件路径需要改成你自己的文件路径)
./redis-server /usr/local/redis/redis.conf
2、创建SpringBoot项目
2.1、配置依赖
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.2、配置application.properties
# 配置redis ip(改成你自己服务器的ip)
spring.redis.host=39.105.232.73
# redis端口
spring.redis.port=6379
# redis密码
spring.redis.password=zhelishimima
# 最大连接数
spring.redis.jedis.pool.max-active=10
# 线程池中最大的空闲连接数
spring.redis.jedis.pool.max-idle=8
# 最小空闲连接数
spring.redis.jedis.pool.min-idle=1
# 连接池最大阻塞时间 -1 代表没有限制
spring.redis.jedis.pool.max-wait=-1
2.3、书写controller
@RestController
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("getValueByKey")
public String getValueByKey(String key) {
String value = userService.getValueByKey(key);
return value;
}
@RequestMapping("addKeyAndValue")
public String addKeyAndValue(String key, String value) {
try {
userService.addKeyAndValue(key, value);
return "添加成功";
} catch (Exception e) {
return "添加失败";
}
}
}
2.4、书写service
public interface IUserService {
/**
* 根据key获取对应的value
*/
String getValueByKey(String key);
/**
* 设置一个key和值
*/
void addKeyAndValue(String key, String value);
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private RedisManager redisManager;
@Override
public String getValueByKey(String key) {
return redisManager.getValueByKey(key);
}
@Override
public void addKeyAndValue(String key, String value) {
redisManager.addKeyAndValue(key, value);
}
}
2.5、书写manager
@Component
public class RedisManager {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 根据key获取对应的value
*/
public String getValueByKey(String key) {
String value = stringRedisTemplate.opsForValue().get(key);
return value;
}
/**
* 设置一个键值对
*/
public void addKeyAndValue(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
}
2.6、测试
分别访问下列路径,正常显示证明redis连接成功
http://127.0.0.1:8080/addKeyAndValue?key=qaz&value=123asd
http://127.0.0.1:8080/getValueByKey?key=qaz
3、常见的API
/**
* 常用的API
* key相关
* 设置key过期的API
* stringRedisTemplate.expire("key", 60, TimeUnit.SECONDS);
* ttl :获取一个键的过期时间
* stringRedisTemplate.getExpire("key");
* 第二个参数表示:返回时间的类型,这里表示 秒
* stringRedisTemplate.getExpire("key", TimeUnit.SECONDS);
* exists :判断一个键是否存在
* stringRedisTemplate.hasKey("key");
* del :删除一个键值对
* stringRedisTemplate.delete("key");
* randomkey :随机返回一个key
* stringRedisTemplate.randomKey()
*
* string类型
* set : 设置值,也可以设置4个参数,过期时间,时间类型
* stringRedisTemplate.opsForValue().set("key", "value");
* get : 获取值
* stringRedisTemplate.opsForValue().get("key");
* mset : 批量设置值
* stringRedisTemplate.opsForValue().multiSet(map<String, String>);
* mget : 批量获取值
* stringRedisTemplate.opsForValue().multiGet(collection<?>);
* incrby :自增
* stringRedisTemplate.opsForValue().increment("key",1);
* decrby :自减
* stringRedisTemplate.opsForValue().increment("key",-1);
*
* hash类型
* hset :设置一个键值对
* stringRedisTemplate.opsForHash().put("key", "field", "value");
* hmset : 批量设置
* stringRedisTemplate.opsForHash().putAll("key", map<?,?>);
* hget : 获取值
* stringRedisTemplate.opsForHash().get("key", "field");
* hmget :
* 获取指定 key 对应的键值对集合,返回结果为 map
* stringRedisTemplate.opsForHash().entries("key");
* 获取指定 key 中field的集合对应的值,返回结果为 list
* stringRedisTemplate.opsForHash().multiGet("key", collection<?>);
* hdel:删除指定hash中 field的值可以传多个
* stringRedisTemplate.opsForHash().delete("key", "field...");
* hexists :判断hash结构中这个键是否存在
* stringRedisTemplate.opsForHash().hasKey()
* hincrby key field increment
* 可为 整型 添加或减少数据
* stringRedisTemplate.opsForHash().increment("key", "field", "long");
* 可为 浮点型 添加或减少数据
* stringRedisTemplate.opsForHash().increment("key", "field", "double");
*
* list类型
* lpop rpop 移除:
* stringRedisTemplate.opsForList().leftPop("key")
* 若没有可移除的,则线程阻塞,2,3参数表示阻塞时间
* stringRedisTemplate.opsForList().leftPop("key", 60, TimeUnit.SECONDS)
* stringRedisTemplate.opsForList().rightPop("key")
* stringRedisTemplate.opsForList().rightPop(key", 60, TimeUnit.SECONDS)
* lpush rpush 添加:
* leftALl, rightAll方法
* stringRedisTemplate.opsForList().rightPush("key", "value");
* stringRedisTemplate.opsForList().rightPushAll("key", "value...");
* stringRedisTemplate.opsForList().rightPushAll("key", collection<String>);
* stringRedisTemplate.opsForList().leftPush("key", "value");
* stringRedisTemplate.opsForList().leftPush("key", "value...");
* stringRedisTemplate.opsForList().leftPush("key", collection<String>);
* stringRedisTemplate.opsForList().rightPush("key", "value");
* stringRedisTemplate.opsForList().rightPush("key", "value");
* 如果存在集合则添加元素:
* stringRedisTemplate.opsForList().leftPushIfPresent("key", "value")
* stringRedisTemplate.opsForList().rightPushIfPresent("key", "value")
* 移除集合中右边的元素,同时在左边加入一个元素; 1:选中的集合;2:添加的值
* stringRedisTemplate.opsForList().rightPopAndLeftPush("key", "value")
* 如果超过等待的时间仍没有元素则退出
* stringRedisTemplate.opsForList().rightPopAndLeftPush("key", "value", 60, TimeUnit.SECONDS)
*
* set类型
* smembers 获取 set 集合中所有的值; Set<String>
* stringRedisTemplate.opsForSet().members("key")
* sismember key val 判断指定集合中是否含有指定的值
* stringRedisTemplate.opsForSet().isMember("key", "value")
* sadd 向指定集合设置值,可设置多个
* stringRedisTemplate.opsForSet().add("key", "value...")
* srem key 删除集合中一个或多个指定的元素
* stringRedisTemplate.opsForSet().remove("key", "value...")
* spop key 删除并返回集合中的一个随机元素
* stringRedisTemplate.opsForSet().pop("key")
* spop key [count] 随机删除指定个数个元素,返回 list
* stringRedisTemplate.opsForSet().pop("key", count)
* sdiff k1 k2 : 返回指定两个集合中的差集
* stringRedisTemplate.opsForSet().difference("key1", "key2")
* sinter k1 k2 : 交集
* stringRedisTemplate.opsForSet().intersect("key1", "key2")
* sunion k1 k2 : 并集
* stringRedisTemplate.opsForSet().union("key1", "key2")
*/
4、运行时可能出现的错误
io.lettuce.core.RedisCommandExecutionException: ERR invalid password
密码错误