我需要初始化需要包含的缓存
String in key
List<Object> in value
Run Code Online (Sandbox Code Playgroud)
所以我有 CacheHelper 类,其中有
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,List<Person>> cacheDataList;
private static final String CACHE_PERSON="cache_key_person";
public CacheHelper() {
}
public void putInCacheFromDb(){
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
}
public void putInList(List<Person> personList){
System.out.println("putting list in cache");
cacheDataList.put(CACHE_PERSON,personList);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在这行代码中,我无法将对象转换为列表 String.class,Person.class,它需要是 String.class,List:
cacheDataList = cacheManager
.createCache("cacheOfPersonList", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,Person.class,
ResourcePoolsBuilder.heap(10)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60,
TimeUnit.SECONDS))));
Run Code Online (Sandbox Code Playgroud)
但我收到错误如下:
Incompatible types. Required Cache<String, List<Person>> but …Run Code Online (Sandbox Code Playgroud) I am new to node and express js.Today I am learning and I have initialized node server as:
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const PORT = 3000
const api=require('./routes/api')
const app = express()
app.use(bodyParser.json())
app.use(cors())
api.use('/api',api)
app.get('/', function(req, res) {
res.send('Hello from server')
})
app.listen(PORT, function(){
console.log("Server running on localhost:" + PORT)
});
Run Code Online (Sandbox Code Playgroud)
I have created a folder routes inside server folder and there is api.js file which has GET method to test, …