我是靠谱客的博主 调皮灰狼,这篇文章主要介绍本地缓存的实现【线程安全的情况下】,现在分享给大家,希望可以做个参考。

方式一:

1.本地缓存:Tomcat服务器内部或JVM当中构建的一个对象,这个对象可以存储很多数据

Tomcat本身就是JVM当中的一个对象【本地缓存指的就是JVM当中内部的缓存】

2.Tomcat运行的本质:Tomcat本身也是一个对象,运行在JVM当中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cn.tedu.basic.net; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; //Tomcat本身也是一个对象,运行在JVM当中 public class Tomcat { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(9999); System.out.println("server is starting"); boolean flag=true; while (flag){ Socket socket = server.accept(); System.out.println("hello "+socket); } server.close(); } }

3.启动类:

复制代码
1
2
3
4
5
6
7
8
9
package com.cy.jt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

4.Controller层

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.cy.jt.demo.controller; import com.cy.jt.demo.service.MemoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; //Memory 缓存 @RestController @RequestMapping("/memory") public class MemoryController { @Autowired private MemoryService memoryService; @GetMapping public List<Map<String,Object>> doRetrieve(){ return memoryService.list(); } }

5.Service层

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.cy.jt.demo.service; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Service public class MemoryService { //本地缓存指的就是JVM当中内部的缓存 //JVM的堆内存中的对象(本地缓存) private Map<String,Object> cache=new ConcurrentHashMap<>();//线程安全 public List<Map<String,Object>> list(){ if(cache.containsKey("memory Key")) {//将来这个key为参数列表的组合 return (List<Map<String,Object>>)cache.get("memory Key"); } System.out.println("Get Data from Database"); //假设如下数据来自于数据库 Map<String,Object> m1=new HashMap<>(); m1.put("id", 100); m1.put("title", "title-A"); Map<String,Object> m2=new HashMap<>(); m2.put("id", 101); m2.put("title", "title-B"); List<Map<String,Object>> list=new ArrayList<>(); list.add(m1); list.add(m2); cache.put("memory Key", list); return list; } }

方式二:

(spring提供的注解方式)

1.启动类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
package com.cy.jt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @EnableCaching//开启缓存的自动化配置 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

2.Controller层:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.cy.jt.demo.controller; import com.cy.jt.demo.service.MemoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; //Memory 缓存 @RestController @RequestMapping("/memory") public class MemoryController { @Autowired private MemoryService memoryService; @GetMapping public List<Map<String,Object>> doRetrieve(){ return memoryService.list(); } }

3.Service层:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.cy.jt.demo.service; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @Service public class MemoryService { //本地缓存指的就是JVM当中内部的缓存 //JVM的堆内存中的对象(本地缓存) // private Map<String,Object> cache=new ConcurrentHashMap<>();//线程安全 @Cacheable(value = "myCache") //由此注解描述的方法为缓存的切入点 //AOP面向切面编程,来实现实现本地缓存数据的存储 public List<Map<String,Object>> list(){ // if(cache.containsKey("memory Key")) {//将来这个key为参数列表的组合 // return (List<Map<String,Object>>)cache.get("memory Key"); // } System.out.println("Get Data from Database"); //假设如下数据来自于数据库 Map<String,Object> m1=new HashMap<>(); m1.put("id", 100); m1.put("title", "title-A"); Map<String,Object> m2=new HashMap<>(); m2.put("id", 101); m2.put("title", "title-B"); List<Map<String,Object>> list=new ArrayList<>(); list.add(m1); list.add(m2); // cache.put("memory Key", list); return list; } }

最后

以上就是调皮灰狼最近收集整理的关于本地缓存的实现【线程安全的情况下】的全部内容,更多相关本地缓存内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部