1.依赖与数据库设置
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-pool2</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.session</groupid> <artifactid>spring-session-data-redis</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=123 #自己的密码 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=-1 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0
2.redis和session配置
@configuration @enablecaching public class redisconfig extends cachingconfigurersupport{ @bean public keygenerator keygenerator() { return new keygenerator() { @override public object generate(object target, method method, object... params) { stringbuilder sb = new stringbuilder(); sb.append(target.getclass().getname()); sb.append(method.getname()); for (object obj : params) { sb.append(obj.tostring()); } return sb.tostring(); } }; } } @configuration @enableredishttpsession(maxinactiveintervalinseconds = 86400*30) public class sessionconfig { }
3.实体与controller层
public class user implements serializable { private static final long serialversionuid = 1l; private long id; private string username; private string password; private string email; private string nickname; private string regtime; public user() { super(); } public user(string email, string nickname, string password, string username, string regtime) { super(); this.email = email; this.nickname = nickname; this.password = password; this.username = username; this.regtime = regtime; } public long getid() { return id; } public void setid(long id) { this.id = id; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getnickname() { return nickname; } public void setnickname(string nickname) { this.nickname = nickname; } public string getregtime() { return regtime; } public void setregtime(string regtime) { this.regtime = regtime; } @override public string tostring() { return "user{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", nickname='" + nickname + '\'' + ", regtime='" + regtime + '\'' + '}'; } }
@restcontroller public class usercontroller { @requestmapping("/getuser") @cacheable(value="user-key") public user getuser() { user user=new user("aa@126.com", "aa", "aa123456", "aa","123"); system.out.println("测试缓存"); return user; } @requestmapping("/uid") string uid(httpsession session) { uuid uid = (uuid) session.getattribute("uid"); if (uid == null) { uid = uuid.randomuuid(); } session.setattribute("uid", uid); return session.getid(); } }
4.运行
@springbootapplication public class redisapplication { public static void main(string[] args) { springapplication.run(redisapplication.class, args); } }
运行结果:
同时也可以用专门的图形界面工具查看:
到此这篇关于springboot使用redis作缓存使用入门的文章就介绍到这了,更多相关springboot redis缓存内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!