首页 文章 精选 留言 我的

精选列表

搜索[java],共10000篇文章
优秀的个人博客,低调大师

Java注解@Cacheable的工作原理

In order to avoid unnecessary query on database it is a common pattern to define a cache in application layer to cache the query result from database. See one example below. Here the application cache is maintained in a custom class CacheContext. public class AccountService1 { private final Logger logger = LoggerFactory.getLogger(AccountService1.class); private CacheContext<Account> accountCacheContext; public Account getAccountByName(String accountName) { Account result = accountCacheContext.get(accountName); if (result != null) { logger.info("get from cache... {}", accountName); return result; } Optional<Account> accountOptional = getFromDB(accountName); if (!accountOptional.isPresent()) { throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName)); } Account account = accountOptional.get(); accountCacheContext.addOrUpdateCache(accountName, account); return account; } In Spring there is an annotation @Cacheable which can make the cache managed by Spring instead of application developer. See improved version: public class AccountService2 { private final Logger logger = LoggerFactory.getLogger(AccountService2.class); @Cacheable(value="accountCache") public Account getAccountByName(String accountName) { logger.info("in method getAccountByName, querying account... {}", accountName); Optional<Account> accountOptional = getFromDB(accountName); if (!accountOptional.isPresent()) { throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName)); } return accountOptional.get(); } In this example, there is no more cache evaluation and cache fill logic. All such stuff are taken over by Spring under the hood and completely transparent to application developer, with the help of following bean configuration in xml: <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="default" /> </bean> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="accountCache" /> </bean> </set> </property> </bean> And how to research what magic has been done by Spring to achieve this behavior?We use the following code to research. It is expected that the request sent by line 31 will directly reach database, while the second request in line 34 will be handled by Spring cache handler. Here in line 31, in debugger we can find that accountService2 is not an instance of application class com.sap.AccountService2, but a dynamic proxy class generated by Spring. As a result after pressing F5, the intercept method of this dynamic proxy class is called: In this method, the execution will be delegated to Spring cache handler class: In example 1, the logic of cache evaluation and fill is done by application, and now it is done in method execute below. First internal cache managed by Spring is checked in line 336 via method findCachedItem. Due to expected cache miss, our application method will be called as usual via reflection, as demonstrated below: After application method to retrieve account from database is done, the result, Account instance with id 2495 is filled to Spring cache, the variable contexts below. Below is the screenshot for Spring internal cache to store application data, which is based on ConcurrentHashMap. Our cached Account instance with id 2495 could be found there. For the second query request issued by application, the cached result will be returned by Spring handler: The last question is, how and when the dynamic proxy is generated?Again let’s go to the entry point of Beans initialization: Here the dynamic proxy is created based on configuration defined in xml with the help of CGlibAopProxy. For more detail about CGlibAopProxy, please refer to Spring official document. 本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

优秀的个人博客,低调大师

BeetlSQL 2.12.28 发布,Java Dao 工具

#I1FBG2 增强批量执行时,保留批量参数 #I1FBSL 修复 LambdaQuery中的orLike,orNotLike,orIsNull,orIsNotNull最后未加空格,导致生成的语句出错 <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>2.12.28.RELEASE</version> </dependency> BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。从2013年来,一直维护,并得到国内公司用户的肯定。 无需注解,自动生成大量内置SQL,轻易完成增删改查功能 数据模型支持Pojo,也支持Map/List这种快速模型,也支持混合模型 SQL 以更简洁的方式,Markdown方式集中管理,同时方便程序开发和数据库SQL调试。 SQL 模板基于Beetl实现,更容易写和调试,以及扩展 内置大量增删改查SQL,内置模板查询,提供Query类等方式避免了手写SQL,及时一个较为复杂的系统, 具备Interceptor功能,可以调试,性能诊断SQL,以及扩展其他功能 内置支持主从数据库,通过扩展,可以支持更复杂的分库分表逻辑 最大程度支持跨数据库平台和重构,开发者所需工作减少到最小

优秀的个人博客,低调大师

BeetlSQL 2.12.21 发布,Java Dao 工具

本次发布修复俩个小Bug Query类无法重复使用的bug,虽然文档提示不能重复使用Query,但有很多人还是用了,所以只好BeetlSQL将其改为可以重复使用 LambdaQuery query =sql.lambdaQuery(User.class); List<User> list = query.andEq(User::getName,'xiandafu').select(); //重复使用 User user = query.andEq(User:getAge,100).single(); 自定义注解@UpdateTime,无法在父类上使用的bug Maven: <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>2.12.21.RELEASE</version> </dependency> BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。从2013年来,一直维护,并得到国内公司用户的肯定。 无需注解,自动生成大量内置SQL,轻易完成增删改查功能 数据模型支持Pojo,也支持Map/List这种快速模型,也支持混合模型 SQL 以更简洁的方式,Markdown方式集中管理,同时方便程序开发和数据库SQL调试。 SQL 模板基于Beetl实现,更容易写和调试,以及扩展 内置大量增删改查SQL,内置模板查询,提供Query类等方式避免了手写SQL,及时一个较为复杂的系统, 具备Interceptor功能,可以调试,性能诊断SQL,以及扩展其他功能 内置支持主从数据库,通过扩展,可以支持更复杂的分库分表逻辑 支持跨数据库平台和NOSQL,开发者所需工作减少到最小。 性能评测,来自https://gitee.com/xiandafu/dao-benchmark

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册