package com.cars.forwardservice.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@Controller
@ResponseBody
//@RestController
public class TestController {
private static Map<String, Object> cache = new HashMap<>();
@GetMapping("/cache/put")
public String testCache(String key,String value){
cache.put(key,value);
// Class<? extends Map> aClass = cache.getClass();
// System.out.println("aClass = " + aClass);
System.out.println("cache = " + cache);
return "ok";
}
@GetMapping("/cache/get")
public Object testCache(String key){
return cache.get(key);
}
}
package com.atguigu.gulimall.search.cache;
import java.util.HashMap;
import java.util.Map;
public class CacheTest {
private static Map<String, Object> cache = new HashMap<>();
public static void main(String[] args) throws InterruptedException {
int cnt = 0;
while (cnt < 5000){
Thread.sleep(1000);
String chchemy = chchemy();
System.out.println(chchemy);
cnt++;
}
}
public static String chchemy() {
String cacheLog = (String) cache.get("cacheLog");
if(cache.get("cacheLog") == null) {
String log = "myCacheLog";
cache.put("cacheLog", log);
return log;
}
return cacheLog;
}
}
标签:Map,缓存,String,cache,本地,cacheLog,import,public From: https://www.cnblogs.com/sensenh/p/17012694.html