Redisson应用
Redisson
依赖
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> </dependency>
schema.sql

drop table users if exists; create table users ( id bigint auto_increment, name varchar(255), age int, create_time timestamp, primary key (id) ); insert into users (name, create_time,age) values ('Lili', now(),29); insert into users (name, create_time,age) values ('Fiona', now(),30); insert into users (name, create_time,age) values ('xyz', now(),30); insert into users (name, create_time,age) values ('zbc', now(),16); insert into users (name, create_time,age) values ('Nana', now(),18);
实体类Users.java

package com.example.demo.model; import lombok.*; import org.hibernate.annotations.CreationTimestamp; import javax.persistence.*; import java.util.Date; @Entity @Table(name = "users") @Data @Builder @ToString(callSuper = true) @NoArgsConstructor @AllArgsConstructor public class Users { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int age; @Column(updatable = false) @CreationTimestamp private Date createTime; }
数据操作接口UsersRepository.java

package com.example.demo.repository; import com.example.demo.model.Users; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; import java.util.List; @RepositoryRestResource(collectionResourceRel = "user", path = "user") public interface UsersRepository extends JpaRepository<Users,Integer> { List<Users> findByName(@Param("name") String name); List<Users> findByNameContaining(@Param("name") String name); }
配置类
package com.example.demo.configure;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RedissonConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedissonClient getRedisson() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port);
return Redisson.create(config);
}
}
控制器
package com.example.demo.controller;
import com.example.demo.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class DemoController {
@Autowired
UsersRepository usersRepository;
@Autowired
private RedissonClient redissonClient;
@RequestMapping("/redis")
public String redis() {
RBucket<String> k1= redissonClient.getBucket("k1");
k1.set("123456");
log.info("k1=",k1.get());
RMap<String,String> map = redissonClient.getMap("users");
usersRepository.findAll().forEach(c -> {
map.put(c.getName(),Integer.toString(c.getAge()));
});
String age = map.get("Fiona");
log.info("Fiona age:"+age);
return "success";
}
}
分布式锁的应用
@RequestMapping("/lock")
public String lock() {
RLock helloLock = redissonClient.getLock("hello");
//加锁
helloLock.lock();
try {
log.info("locked");
Thread.sleep(1000 * 200);
} catch (InterruptedException e){
log.error("error",e.getMessage());
} finally {
//释放锁
helloLock.unlock();
}
log.info("finished");
return "success";
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2018-04-08 基于thinkphp的API日志
2016-04-08 查看mysql语句运行时间