概述
方式一:
1.本地缓存:Tomcat服务器内部或JVM当中构建的一个对象,这个对象可以存储很多数据
Tomcat本身就是JVM当中的一个对象【本地缓存指的就是JVM当中内部的缓存】
2.Tomcat运行的本质:Tomcat本身也是一个对象,运行在JVM当中
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.启动类:
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层
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层
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.启动类:
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层:
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层:
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;
}
}
最后
以上就是调皮灰狼为你收集整理的本地缓存的实现【线程安全的情况下】的全部内容,希望文章能够帮你解决本地缓存的实现【线程安全的情况下】所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复