Edit profile redis.proper
explain : Because this configuration is used by other projects , It should be written that common in .
Edit configuration class
explain : edit redis Configuration class , take Jedis Object to Spring Container management .
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
@Bean
public Jedis jedis(){
return new Jedis(host,port);
}
}
Object and the JSON conversion (ObjectMapper Introduce )
Simple object transformation
/**
* Test the transformation of simple objects
*/
@Test
public void test01() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
ItemDesc itemDesc = new ItemDesc();
itemDesc.setItemId(100L).setItemDesc(" Product details ")
.setCreated(new Date()).setUpdated(new Date());
// Object to json
String json = objectMapper.writeValueAsString(itemDesc);
System.out.println(json);
//json Convert to object
ItemDesc itemDesc2 = objectMapper.readValue(json, ItemDesc.class);
System.out.println(itemDesc2.getItemDesc());
}
Set object conversion
// Test the transformation of collection objects
@Test
public void testJson01() throws JsonProcessingException {
ObjectMapper objectMapper=new ObjectMapper();
ItemDesc itemDesc1=new ItemDesc();
itemDesc1.setItemId(100L).setItemDesc(" Product information details 1")
.setCreated(new Date()).setUpdated(new Date());
ItemDesc itemDesc2=new ItemDesc();
itemDesc2.setItemId(100L).setItemDesc(" Product information details 2")
.setCreated(new Date()).setUpdated(new Date());
List<ItemDesc> list=new ArrayList<>();
list.add(itemDesc1);
list.add(itemDesc2);
// Convert the collection object to json Format
String json = objectMapper.writeValueAsString(list);
System.out.println(json);//[{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":" Product information details 1"},{"created":1604828831646,"updated":1604828831646,"itemId":100,"itemDesc":" Product information details 2"}]
// take json Format strings are converted to collection objects
List list1 = objectMapper.readValue(json, list.getClass());
System.out.println(list1);//[{created=1604828831646, updated=1604828831646, itemId=100, itemDesc= Product information details 1}, {created=1604828831646, updated=1604828831646, itemId=100, itemDesc= Product information details 2}]
}
}
Editing tools API
public class ObjectMapperUtil {
/**
* 1. Convert the data passed by users into json
* 2. Pass the user json Convert to object
*/
private static final ObjectMapper MAPPER=new ObjectMapper();
//1. Convert the data passed by users into json
public static String toJson(Object object){
if(object==null){
throw new RuntimeException(" The data passed cannot be empty , Please check ");
}
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
// Convert a check exception to a runtime exception
e.printStackTrace();
throw new RuntimeException(e);
}
}
//2. Pass the user json Convert to object
// demand : What kind of type is required from the user , What kind of object is returned ( Using knowledge of generics )
public static <T> T toObj(String json,Class<T> target){
if(StringUtils.isEmpty(json)||target==null)
throw new RuntimeException(" The parameter cannot be null ");
try {
return MAPPER.readValue(json, target);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
Cache implementation of commodity classification
Implementation steps
1. Definition Redis Medium key.key It must be the only one that cannot repeat , Design to storage and retrieval .【 The format should be :key="ITEM_CAT_PARENTID::70"】
2. according to key Go to redis Query data in ( There's data | No data ).
3. If there is no data, query the database to get Julu and save the query data to redis in , Convenient for subsequent use .
4. There is data indicating that the user is not querying for the first time , You can return the cached data directly .
edit ItemCatController
edit ItemCatService
/**
* 1. Definition Redis Medium key,
* there key The only request can't be repeated , It's about saving and taking
* key="ITEM_CAT_PARENTID::"+parentId;
* 2. according to key Go to redis Query in Either there's data Or there's no data
* 3. If there is no data, query the database for records , Then save the data in redis in , Convenient for subsequent use
* 4. If there is data, it means that the user is not querying for the first time , You can also directly return the cached data
*
*/@Override
public List<EasyUITree> findItemCatListCache(Long parentId) {
// Define a common return value object
List<EasyUITree> treeList=new ArrayList<>();
//1. Definition Redis Medium key,
String key="ITEM_CAT_PARENTID::"+parentId;
// test redis The execution time and query database time in
Long startTime= System.currentTimeMillis();
// retrieval redis Medium key Whether there is
if(jedis.exists(key)){
// Data exists
String json=jedis.get(key);
// Need to put json String is converted to an object , And store the transformed object into treeList
treeList= ObjectMapperUtil.toObj(json, treeList.getClass());
Long endTime= System.currentTimeMillis();
System.out.println("redis execution time "+(endTime-startTime));
}else{
// The data doesn't exist Then call the above query method to query data in the database
treeList= findItemCatList(parentId);
// Save data to cache , In order to facilitate the follow-up query
String json = ObjectMapperUtil.toJson(treeList);
jedis.set(key,json);
Long endTime= System.currentTimeMillis();
System.out.println(" Database query execution time "+(endTime-startTime));
}
return treeList;
}