# blog **Repository Path**: liwen135/blog ## Basic Information - **Project Name**: blog - **Description**: blog,国际化(i18n),缓存(spring cache),全文检索(lucene) - **Primary Language**: Java - **License**: Not specified - **Default Branch**: v2 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 94 - **Created**: 2018-04-10 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # v2版本(前后端分离) ## 前端 [article-front](https://gitee.com/TIM-JT/article-front) ## 国际化 后端:国际化 主要是针对返回结果的提示信息,spring-webmvc 中的i18n去实现 - 1、spring.messages.basename=i18n/messages在spplication.properties中配置国际化的目录 - 2、LocaleResolver地区处理器,项目使用了AcceptHeaderLocaleResolver,通过请求头中的Accept-Language来识别地区语言 - 3、zh-CN表示中文-中国,en-US表示英文-美国 eg 配置 @Bean public LocaleResolver localeResolver() { AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver(); resolver.setDefaultLocale(Locale.CHINESE); return resolver; } 使用 //国际化资源 @Autowired private MessageSource messageSource; public static void main(){ //从上下文中获取地区 Locale locale = LocaleContextHolder.getLocale(); int key = 100; String msg = messageSource.getMessage(key + "", null, locale); } ## 缓存 jar依赖 org.springframework.boot spring-boot-starter-cache net.sf.ehcache ehcache 使用详解 注:缓存注解只对public方法有效,其他类型均无效;Spring代理模式下对象内部自己调用@Cacheable方法不会使用Cache - 1、@EnableCaching 配置在Application类上,开启 spring cache,会扫描spring bean是否存在注解对应的缓存,生成代理拦截方法 - 2、@CacheConfig(cacheNames = "_category") 配置在业务处理类上,标注该业务处理类对应的缓存cache 名称 - 3、@Cacheable(key = "'_id' + #id") 配置在查询方法上,当缓存存在时会从缓存中查询,反之则从数据库查询,并置入缓存中,#id 表示使用方法参数id,key 用于标识查询的缓存value所对应的key - 4、@CachePut(key = "'_id' + #category.id") 配置在保存、更新方法上,保存或替换缓存 - 5、@CacheEvict(key = "'_id' + #id") 配置在删除方法上,删除缓存 ehcache 使用原因 - 1、 为了方便项目部署,就不使用redis了 - 2、 本来打算使用guava,但发现spring boot 2.0系列 spring cache 不支持 guava,没有仔细去研究(标记) ehcache.xml - 1、maxEntriesLocalHeap, ehcache 2+ 之后maxElementsInMemory被maxEntriesLocalHeap取代了,表示在堆内存中存储最大的个数 - 2、timeToIdleSeconds(允许空闲时间)、timeToLiveSeconds(允许生存时间), 两个都配置之后 取 两个时间的最小值 - eg. timeToIdleSeconds=120、timeToLiveSeconds=300;当对象总存在时间超过 5 分钟、当对象存在时间小于5分钟,但空闲时间已达到2分钟都会被销毁 ## 全文检索 对文章的扫描主要是针对 title、summary、tags 三个字段,可以用 布尔查询 或者 多字段查询,具体不展开了 lucene jar依赖 org.apache.lucene lucene-queryparser 6.6.0 org.apache.lucene lucene-highlighter 6.6.0 中文分词器jar依赖 com.hankcs hanlp portable-1.2.8 com.hankcs hanlp-solr-plugin 1.0.3 system ${project.basedir}/libs/hanlp-solr-plugin-1.0.3.jar IndexWriter、DirectoryReader 生命周期 - 1、indexWriter 打开之后后面会产生一个上锁文件.lock - 2、indexWirter 通过 commit 和 rollback 实现事务的一致性 - 3、DirectoryReader open 之后,如果indexWriter commit 之后,需要重新打开reader才能获取到新更新的索引信息 - 4、DirectoryReader openIfChanged 方法当索引更新后,会返回新的reader