当前位置:网站首页>< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (10)
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (10)
2022-04-21 13:54:00 【indaeyo】
2021SC@SDUSC
This article focuses on code modules jpress-service,jpress-service-provider, analysis menuService、menuServiceProvider Two classes . Because this kind of design is at the bottom JFinal、Jboot Secondary development and use of various caches , So I will add some cache related knowledge , So that we can better understand the code .
List of articles
One 、MenuService
1.1 menu Database table
Through the attributes of the table in the database, it is more conducive to understand the relevant operations in the function . Here are menu surface :

1.2 MenuService
MenuService Defines the relevant interface of menu operation , stay provider Will give detailed implementation , Here briefly describe the purpose of the interface .
Menu findById(Object id);
adopt id Value to find the corresponding menu
List<Menu> findAll();
Query all menu data
boolean delete(Menu model);
adopt menu Type of model Delete menu data
Object saveOrUpdate(Menu model);
Add or update Model data , After adding or updating successfully , Return to the Model The primary key value
int sync(List<Menu> menus);
Achieve menu synchronization
List<Menu> findListByParentId(Object id);
Through the parent menu ID Find the submenu list
List<Menu> findListByRelatives(String table, Object id);
Find the list of submenus through the associated table , There are... In the database table definition of the menu relative_table One value 、
Two 、Cache cache
First, show the part MenuServiceProvider Code for :
@Cacheable(name = "menu", key = "type:#(type)")
public List<Menu> findListByTypeInDb(String type) {
return DAO.findListByColumn(Column.create("type", type), "order_number asc, id desc");
}
We can see , If you don't understand the purpose and use of annotations , Can't have a good interpretation of the code , Next, we will first analyze the code of the caching mechanism , Proceed again MenuServiceProvider Code analysis of .
import io.jboot.components.cache.annotation.CacheEvict;
import io.jboot.components.cache.annotation.Cacheable;
MenuServiceProvider Use Jboot Provide annotations to use the caching mechanism .
2.1 Cache description
Jboot Positioned as a high-performance microservice framework , However, high performance is inseparable from reasonable cache design .Jboot Built in rich framework support , such as :
- ehcache
- redis
- ehredis
- caffeine
- caredis
- j2cache
By default Jboot It's using caffeine As Jboot Caching scheme for
JFinal The default cache implementation in is ehcache, Need to introduce ehcache Of jar Package and its configuration file
EhCache It's pure. Java In process caching framework for , With fast 、 Characteristics such as ability , yes Hibernate The default CacheProvider.Ehcache Is a widely used open source Java Distributed cache . Mainly for general purpose cache ,Java EE And lightweight containers . It has memory and disk storage , Cache loader , Cache extension , Cache exception handler , One gzip cache servlet filter , Support REST and SOAP api Other characteristics .
In terms of integration , Can be used alone , It is generally used in third-party libraries ( Such as mybatis、shiro etc. )ehcache Not good enough for distributed support , Multiple nodes cannot be synchronized , Usually and redis Use it together .
2.2 Using cache via annotations
- @Cacheable
- @CacheEvict
- @CachesEvict
- @CachePut
stay service in ,Jboot Provides the above 4 A component , It is convenient for us to cache , Without having to use CacheUtil To display the call .
for example :
@Bean
public class CommentServiceImpl implements CommentService {
@Override
public String getCommentById(String id) {
return "id:" + id + " data:" + UUID.randomUUID();
}
@Override
@Cacheable(name = "cacheName", key = "#(id)")
public String getCommentByIdWithCache(String id) {
return "id:" + id + " data:" + UUID.randomUUID();
}
@Override
@Cacheable(name = "cacheName", key = "#(id)", liveSeconds = 5)
public String getCommentByIdWithCacheTime(String id) {
return "id:" + id + " data:" + UUID.randomUUID();
}
@Override
@CachePut(name = "cacheName", key = "#(id)")
public String updateCache(String id) {
return "id:" + id + " data:" + UUID.randomUUID();
}
@Override
@CacheEvict(name = "cacheName", key = "#(id)")
public void delCache(String id) {
}
}
getCommentByIdMethod does not use any annotations , Every time it's called ,data What follows is a new random number .getCommentByIdWithCacheUse@Cacheableannotation , The cache key It came in id, So as long as it's the same id value , The random number returned each time is the same , Because random numbers have been cached .getCommentByIdWithCacheTimeUse@Cacheableannotation , But added5 secondTime limit of , therefore , stay 5 In seconds , No matter how many times you call , The random numbers returned are the same ,5 Seconds later, the cache is deleted , After calling again, there will be a new random number , The new random number will continue to be cached 5 Second .updateCacheUsing annotations@CachePut, After each call to this method , It will be updated id Value cache .delCacheUsed@CacheEvictannotation , Each call deletes the id Value cache .
3、 ... and 、MenuServiceProvider
Realized menuService The important function in , To the top Controller Provide interface for menu operation . Next, we will provider Detailed analysis of the main method .
3.1 shouldUpdateCache
@Override
@CacheEvict(name = "menu", key = "*")
public void shouldUpdateCache(int action, Model model, Object id) {
super.shouldUpdateCache(action, model, id);
}
This method updates the cache , Input action Parameter values for , When the function is called , Will delete the id Value cache , Achieve the update effect . But because key The choice is “*”, So whatever it is id value , Will be in the cache with the name menu Objects deleted from the cache .
3.2 findListByType
@Override
public List<Menu> findListByType(String type) {
return ModelUtil.copy(findListByTypeInDb(type));
}
// The method called in the above method
@Cacheable(name = "menu", key = "type:#(type)")
public List<Menu> findListByTypeInDb(String type) {
return DAO.findListByColumn(Column.create("type", type), "order_number asc, id desc");
}
The purpose of this method is to type Type find a list of this type .
First, through DAO Find the whole list , By order number and id Arrange in order .
Next use @Cacheable annotation , take key by type Information stored in the cache .
Four 、 summary
In this article , This paper introduces the relevant important methods of menu function , It also introduces the cache mechanism in JPress Through the use and implementation of annotations in the project . In the next article , The explicit call and implementation of caching mechanism will be introduced .
版权声明
本文为[indaeyo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211348187048.html
边栏推荐
- Notes d'examen triées
- CDH5删除数据节点
- Unittest单元测试(二)
- EsgynDB 使用JDBC UDR访问远程Trafodion的几个问题小结
- 2021-10-19性能测试
- 深度学习与图像识别:原理与实践 笔记Day_18(目标检测)续
- 数字签名,RSA的公私钥加密与解密
- Basic training process of image classification -- Based on mobilenet_ V3 as an example
- 《商用密码应用与安全性评估》第四章 密码应用安全性评估实施要点-小结
- Deep understanding of concurrent programming
猜你喜欢

Unittest unit test (III)

EsgynDB CQD-traf_lock_ddl

【leetcode】516.最长回文子序列

STM32单片机初学5-IIC通信驱动OLED屏幕

Software testing common problems development model PC QQ login test case bug related problems test case design common methods

The sales attribute values in sku must be filled in in pairs. Why

电力系统相关知识

EsgynDB CQD-traf_ lock_ ddl

JVM内存模型深度剖析与优化

Pits to be avoided when Networkx and pyg calculate degree degree: self loop and multiple edges
随机推荐
【leetcode】516. Longest palindrome subsequence
[special topic of stack and queue] - Dual queue simulation stack
Unittest unit test (II)
New technology is coming again, embrace agp7 0, are you ready to say goodbye to transform?
Oracle 备份与用户解锁
mysql-慢查询日志与索引合并解析
NPM - environment
Trafodion troubleshooting - error [1034] unable to obtain privileges
Sqlyog import SQL file
Machine learning notes - SVD singular value decomposition (3) applying SVD to images
深度学习与图像识别:原理与实践 笔记Day_18(目标检测)续
Initial view of SQL level hint function of the new features of Yijing jieqian bank
2021-10-21软件测试理论
认识系统服务
Analysis of MySQL connection query cost and cost statistics
The importance of computing edge in Networkx: edge intermediate number or intermediate centrality edge_ betweenness
深度学习与图像识别:原理与实践 笔记Day_13
<2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(四)
Software testing common problems development model PC QQ login test case bug related problems test case design common methods
Oracle实例的管理