Overall project introduction:
https://blog.csdn.net/wenjieyatou/article/details/80190886
Coupon Item 1 Introduction:https://blog.csdn.net/wenjieyatou/article/details/80191083
Coupon Item 2 Introduction:
https://blog.csdn.net/wenjieyatou/article/details/80203860
Coupon project three introduction:
https://blog.csdn.net/wenjieyatou/article/details/80206069
Coupon project four introduction:
https://blog.csdn.net/wenjieyatou/article/details/80207217
The following is an introduction to the five coupon items:
1: Redis is used as a cache, and the activities that are valid on the day are taken, and the coupon group is placed in the event. The 500 coupons under the coupon group are stored in the cache.
2: Add timed tasks and update the cache at 12:00 every day (this time can be monitored by hotspot data)
3: The statistical results found:
After adding the cache, it takes only 2.7s to send 500 coupons, which is 2.1s faster than the previous 4.8s, which greatly improves the performance.
1: Redis is used as a cache, and the activities that are valid on the day are taken, and the coupon group is placed in the event. The 500 coupons under the coupon group are stored in the cache.
import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.migr.common.util.JsonUtil; import com.migr.common.util.StringUtils; import com.peiyu.mem.dao.CpActivityDao; import com.peiyu.mem.dao.CpActsubGroupDao; import com.peiyu.mem.dao.CpapplylimitdtDao; import com.peiyu.mem.dao.CpuselimitdtDao; import com.peiyu.mem.domian.entity.CpActivity; import com.peiyu.mem.domian.entity.CpActsubGroup; import com.peiyu.mem.domian.entity.CpApplyLimitdt; import com.peiyu.mem.domian.entity.CpUseLimitdt; import com.peiyu.mem.redis.JedisTemplate; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/1/5. */ @Service public class CouponActivityCacheManager { private Logger log = Logger.getLogger(CouponActivityCacheManager.class); @Autowired private CpActivityDao activityDao; @Autowired private CpActsubGroupDao actsubGroupDao; @Autowired private CpapplylimitdtDao applyLimitdtDao; @Autowired private CpuselimitdtDao cpuselimitdtDao; @Autowired private JedisTemplate jedisTemplate; @Autowired private ThreadPoolTaskExecutor taskExecutor; abstract class ListGetterHandler<T> { /** * Load data from library * * @param search * @return */ abstract List<T> getListFromDB(T search); /** * Get data from cache if not loaded from library, and flush cache * * @param key * @param search * @return */ public List<T> getListAndSetCache(final String key, T search, Class<T> type) { List<T> list = new ArrayList<>(); String jsonList = jedisTemplate.get(key); if (StringUtils.isBlank(jsonList)) { final String lockKey = "lock:" + key; if (jedisTemplate.hsetNX(lockKey, lockKey, "60") == 1) {//Prevent the database from being broken down during concurrency (indicating that the setting is successful) list = getListFromDB(search); final List<T> finalList = list; // refresh the cache asynchronously taskExecutor.execute(new Runnable() { @Override public void run() { try { String json = JsonUtil.g.toJson(finalList); jedisTemplate.set(key, json, 24 * 60 * 60); } catch (Exception e) { log.error("Failed to flush cache", e); } finally { jedisTemplate.hdel(lockKey, lockKey); } } }); } } if (StringUtils.isNotBlank(jsonList)) { JsonArray array = new JsonParser().parse(jsonList).getAsJsonArray(); for (final JsonElement elem : array) { list.add(JsonUtil.g.fromJson(elem, type)); } return list; } return list; } } private ListGetterHandler<CpActivity> activityListGetterHandler = new ListGetterHandler<CpActivity>() { @Override List<CpActivity> getListFromDB(CpActivity search) { return activityDao.getCpActivityBySearch(search); } }; private ListGetterHandler<CpActsubGroup> actsubGroupListGetterHandler = new ListGetterHandler<CpActsubGroup>() { @Override List<CpActsubGroup> getListFromDB(CpActsubGroup search) { return actsubGroupDao.getCpActsubGroupList(search); } }; private ListGetterHandler<CpApplyLimitdt> applyLimitdtListGetterHandler = new ListGetterHandler<CpApplyLimitdt>() { @Override List<CpApplyLimitdt> getListFromDB(CpApplyLimitdt search) { return applyLimitdtDao.getCpApplyLimitdtsBySearch(search); } }; private ListGetterHandler<CpUseLimitdt> useLimitdtListGetterHandler = new ListGetterHandler<CpUseLimitdt>() { @Override List<CpUseLimitdt> getListFromDB(CpUseLimitdt search) { return cpuselimitdtDao.getCpUseLimitdts(search); } }; /** * Get coupons that are valid on the day * * @param search * @return */ public List<CpActivity> getCpActivityList(CpActivity search) { String key = String.format("activitys_%s_%s", search.getVendorId(), search.getSendType()); return activityListGetterHandler.getListAndSetCache(key, search, CpActivity.class); } /** * Get valid coupons under the event * * @param search * @return */ public List<CpActsubGroup> getCpActsubGroupList(CpActsubGroup search) { String key = String.format("actSubGruops_%s_%s", search.getVendorId(), search.getActNo()); return actsubGroupListGetterHandler.getListAndSetCache(key, search, CpActsubGroup.class); } /** * Get the application scope limit of the activity * * @param search * @return */ public List<CpApplyLimitdt> getCpApplyLimitdtList(CpApplyLimitdt search) { String key = String.format("applyLimits_%s_%s", search.getVendorId(), search.getOwnRecordCode()); return applyLimitdtListGetterHandler.getListAndSetCache(key, search, CpApplyLimitdt.class); } /** * Get activity usage limits * @param search * @return */ public List<CpUseLimitdt> getCpUseLimitList(CpUseLimitdt search) { String key = String.format("useLimits_%s_%s", search.getVendorId(), search.getOwnRecordCode()); return useLimitdtListGetterHandler.getListAndSetCache(key, search, CpUseLimitdt.class); } }
2: Add timed tasks and update the cache at 12:00 every day (this time can be monitored by hotspot data)
package com.peiyu.mem.service.impl; import com.migr.common.util.JsonUtil; import com.peiyu.mem.commen.SysConstants; import com.peiyu.mem.dao.CpActivityDao; import com.peiyu.mem.dao.CpActsubGroupDao; import com.peiyu.mem.dao.CpapplylimitdtDao; import com.peiyu.mem.dao.CpuselimitdtDao; import com.peiyu.mem.domian.entity.CpActivity; import com.peiyu.mem.domian.entity.CpActsubGroup; import com.peiyu.mem.domian.entity.CpApplyLimitdt; import com.peiyu.mem.domian.entity.CpUseLimitdt; import com.peiyu.mem.redis.JedisTemplate; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; /** * Created by Administrator on 2017/1/10. * Coupon timing task */ @Service public class ActivityTimingTask { @Autowired private CpActivityDao activityDao; @Autowired private CpActsubGroupDao actsubGroupDao; @Autowired private CpapplylimitdtDao cpapplylimitdtDao; @Autowired private CpuselimitdtDao cpuselimitdtDao; @Autowired private JedisTemplate jedisTemplate; /** * Get data proxy * * @param <T> */ abstract class CacheRefreshHandler<T> { /** * Database load data * * @param search * @return */ abstract List<T> getListFromDB(T search); /** * Get key value * * @param item * @return */ abstract String getKey(T item); /** * Refresh activity general method * * @param search */ public void refreshCache(T search) { List<T> list = getListFromDB(search); if (CollectionUtils.isEmpty(list)) { return; } String key = getKey(search); jedisTemplate.set(key, JsonUtil.g.toJson(search)); } } public void TimingRefreshAllCache(){ try { this.refreshActivityCache(new CpActivity()); this.refreshSubGroupCache(new CpActsubGroup()); this.refreshCpApplyLimitdt(new CpApplyLimitdt()); this.refreshCpUseLimitdt(new CpUseLimitdt()); }catch (Exception e){ System.out.println("Failed to flush cache"); } } /** * Refresh the current day's event cache * * @param search */ protected void refreshActivityCache(CpActivity search) { search.setEndDate(new Date()); search.setStatus(SysConstants.ACTIVITYSTATUS.CHECKED); CacheRefreshHandler<CpActivity> refreshHandler = new CacheRefreshHandler<CpActivity>() { @Override List<CpActivity> getListFromDB(CpActivity search) { return activityDao.getCpActivityBySearch(search); } @Override String getKey(CpActivity item) { return String.format(""); } }; refreshHandler.refreshCache(search); } /** * Refresh the cache of coupon groups * * @param search */ public void refreshSubGroupCache(CpActsubGroup search) { CacheRefreshHandler<CpActsubGroup> actsubGroupCacheRefreshHandler = new CacheRefreshHandler<CpActsubGroup>() { @Override List<CpActsubGroup> getListFromDB(CpActsubGroup search) { return actsubGroupDao.getCpActsubGroupList(search); } @Override String getKey(CpActsubGroup item) { return String.format(""); } }; actsubGroupCacheRefreshHandler.refreshCache(search); } /** * Refresh application scope limit * @param search */ public void refreshCpApplyLimitdt(CpApplyLimitdt search){ CacheRefreshHandler<CpApplyLimitdt> cacheRefreshHandler=new CacheRefreshHandler<CpApplyLimitdt>() { @Override List<CpApplyLimitdt> getListFromDB(CpApplyLimitdt search) { return cpapplylimitdtDao.getCpApplyLimitdtsBySearch(search); } @Override String getKey(CpApplyLimitdt item) { return String.format(""); } }; cacheRefreshHandler.refreshCache(search); } /** * Refresh usage limits * @param search */ public void refreshCpUseLimitdt(CpUseLimitdt search){ CacheRefreshHandler<CpUseLimitdt> cacheRefreshHandler=new CacheRefreshHandler<CpUseLimitdt>() { @Override List<CpUseLimitdt> getListFromDB(CpUseLimitdt search) { return cpuselimitdtDao.getCpUseLimitdts(search); } @Override String getKey(CpUseLimitdt item) { return String.format(""); } }; cacheRefreshHandler.refreshCache(search); } }