定时任务(刷新memcache缓存)
2019-04-15 18:26
197 查看
定时任务(刷新memcache缓存)
xml配置文件spring-job.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 配置注解扫描 --> <context:annotation-config /> <!-- 自动扫描的包名 --> <context:component-scan base-package="com.yhfund.wx" /> <task:scheduler id="springScheduler" pool-size="1" /> <task:scheduled-tasks scheduler="springScheduler"> <task:scheduled ref="cacheJob" method="refreshFundInfo" fixed-rate="3600000" /> </task:scheduled-tasks> </beans>
注:
cacheJob 执行任务的java类名
refreshFundInfo 执行任务的方法(cacheJob 中的refreshFundInfo方法 )
3600000 刷新频率(毫秒)
查询数据库并将结果放入memcache(键值对)
package com.yhfund.wx.quartzjob; import com.yhfund.api.middleware.exception.MiddlewareException; import com.yhfund.api.middleware.response.query.FundInfo; import com.yhfund.app.tools.utils.JsonMapper; import com.yhfund.wx.constants.CacheConstants; import com.yhfund.wx.service.MemCacheService; import net.rubyeye.xmemcached.MemcachedClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class CacheJob { private static final Logger logger = LoggerFactory.getLogger(CacheJob.class); private static final JsonMapper normalJsonMapper = JsonMapper.buildNormalMapper(); @Autowired private MemCacheService memCacheService; @Autowired private MemcachedClient memcachedClient; public void refreshFundInfo(){ try { List<FundInfo> resultList = memCacheService.queryFundInfoList();//查数据库过程 memcachedClient.set(key, cachetime, value); logger.debug("刷新成功:"+ normalJsonMapper.toJson(resultList)); } catch (MiddlewareException appe) { logger.debug("刷新失败", appe); } catch (Exception e) { e.printStackTrace(); logger.debug("刷新失败", e); } } }
相关文章推荐
- Guava Cache内存缓存使用实践-定时异步刷新及简单抽象封装
- APP接口的核心技术——缓存和定时任务
- HTTP解读-文件下载、中文乱码、请求重定向、定时刷新、缓存、url编解码、gzip压缩、防盗链等
- 定时任务10秒刷新
- spring+quartz实现定时任务(每天零点删除指定文件夹中缓存文件)
- Guava Cache内存缓存使用实践-定时异步刷新及简单抽象封装
- 帝国CMS-前台定时执行刷新任务插件下载
- HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)
- 21世纪网:利用meta实现定时刷新,清除缓存保证每次加载最新数据等功能
- AspNet Core结合Quartz使用定时任务且通过注入缓存或者配置参数
- Response_输出数据,实现文件下载,定时刷新页面,是否缓存,重定向,实现验证码
- HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)
- 禁止浏览器缓存的响应头和定时刷新
- JavaWeb_Day10_学习笔记1_response(3、4、5、6、7、8、9)发送状态码、响应、重定向、定时刷新、禁用浏览器缓存、响应字节数据、快捷重定向方法、完成防盗链
- 控制浏览器定时刷新、缓存、重定向
- 定时刷新lucene缓存配置方式
- 140服务器经过一周的观察,缓存上升太快所以需要每天都要清理缓存,所以添加定时任务,每天凌晨清理缓存
- Guava Cache内存缓存使用实践-定时异步刷新及简单抽象封装
- golang根据配置的时间和时区计算定时任务是否到了刷新时间
- Spring定时(任务)刷新-quartz