소스 검색

【定时任务】统计前一天商品详情访问量

chao 3 년 전
부모
커밋
1e880ead26
2개의 변경된 파일17개의 추가작업 그리고 318개의 파일을 삭제
  1. 0 307
      src/main/java/com/caimei365/tools/service/RedisService.java
  2. 17 11
      src/main/java/com/caimei365/tools/task/StatisticsTask.java

+ 0 - 307
src/main/java/com/caimei365/tools/service/RedisService.java

@@ -1,307 +0,0 @@
-package com.caimei365.tools.service;
-
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.data.redis.core.HashOperations;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.ValueOperations;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.Resource;
-import java.io.Serializable;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Redis 服务工具类
- *
- * @author : Charles
- * @date : 2021/3/4
- */
-@Slf4j
-@Service
-public class RedisService {
-
-	@Resource
-	private RedisTemplate<Serializable, Object> redisTemplate;
-
-    /**
-     * 批量删除
-     * @param keys
-     */
-	public void remove(String... keys) {
-		for(String key :keys){
-			remove(key);
-		}
-	}
-
-    /**
-     * 批量删除正则匹配到的
-     * @param pattern
-     */
-	public void removePattern(String pattern) {
-		Set<Serializable> keys = redisTemplate.keys(pattern);
-        assert keys != null;
-        if (keys.size() > 0){
-			redisTemplate.delete(keys);
-		}
-	}
-
-    /**
-     * 删除
-     * @param key
-     */
-	public void remove(String key) {
-		if (exists(key)) {
-			redisTemplate.delete(key);
-		}
-	}
-
-    /**
-     * 判断缓存中是否存在
-     * @param key
-     * @return boolean
-     */
-	public boolean exists(String key) {
-		return StringUtils.isBlank(key) ? false : redisTemplate.hasKey(key);
-	}
-
-    /**
-     * 读取缓存
-     * @param key
-     * @return
-     */
-	public Object get(String key) {
-		Object result = null;
-		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
-		result = operations.get(key);
-		return result;
-	}
-
-	/**
-	 * 读取缓存hash【hgetall】
-	 * @param key
-	 * @return
-	 */
-	public Map<Object, Object> hgetall(String key) {
-		Map<Object, Object> result = null;
-		HashOperations<Serializable, Object, Object> operations = redisTemplate.opsForHash();
-		result = operations.entries(key);
-		return result;
-	}
-
-    /**
-     * 写入缓存
-     * @param key
-     * @param value
-     * @return
-     */
-	public boolean set(String key, Object value) {
-		boolean result = false;
-		try {
-			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
-			operations.set(key, value);
-			result = true;
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return result;
-	}
-
-    /**
-     * 写入缓存并加上过期时间(秒)
-     * @param key
-     * @param value
-     * @param expireTimeSeconds
-     * @return
-     */
-	public boolean set(String key, Object value, Long expireTimeSeconds) {
-		boolean result = false;
-		try {
-			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
-			operations.set(key, value);
-			redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
-			result = true;
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return result;
-	}
-    /**
-     * 写入过期时间(秒)
-     * @param key
-     * @param expireTimeSeconds
-     * @return
-     */
-	public boolean expire(String key, Long expireTimeSeconds) {
-		boolean result = false;
-		try {
-			redisTemplate.expire(key, expireTimeSeconds, TimeUnit.SECONDS);
-			result = true;
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		return result;
-	}
-    /* **************************** 针对list操作的方法 **************************** */
-    /**
-     * 在key对应list的尾部添加
-     * @param key
-     * @param value
-     * @return
-     */
-	public long rightPushForList(String key, Object value) {
-		return redisTemplate.opsForList().rightPush(key, value);
-	}
-
-    /**
-     * 在key对应list的头部添加
-     * @param key
-     * @param value
-     * @return
-     */
-	public long leftPushForList(String key, Object value) {
-		return redisTemplate.opsForList().leftPush(key, value);
-	}
-
-    /**
-     * key对应list的长度
-     * @param key
-     * @return
-     */
-	public long listSize(String key) {
-		return redisTemplate.opsForList().size(key);
-	}
-
-    /**
-     * 获取list集合
-     * @param Key
-     * @param begin
-     * @param end
-     * @return
-     */
-	public List<?> getList(String Key, int begin, int end) {
-		return redisTemplate.opsForList().range(Key, begin, end);
-	}
-
-    /**
-     * 在key对应list的尾部移除
-     * @param key
-     * @return
-     */
-	public Object rightPopForList(String key) {
-		return redisTemplate.opsForList().rightPop(key);
-	}
-
-    /**
-     * 在key对应list的头部移除
-     * @param key
-     * @return
-     */
-	public Object leftPopForList(String key) {
-		return redisTemplate.opsForList().leftPop(key);
-	}
-
-    /**
-     * 移除list的index位置上的值
-     * @param Key
-     * @param index
-     * @param value
-     */
-	public void removeList(String Key, long index, Object value) {
-		redisTemplate.opsForList().remove(Key, index, value);
-	}
-
-    /**
-     * 重置list的index位置上的值
-     * @param Key
-     * @param index
-     * @param value
-     */
-	public void setList(String Key, long index, Object value) {
-		redisTemplate.opsForList().set(Key, index, value);
-	}
-
-
-    /**
-     * 写入list
-     * @param key
-     * @param list
-     */
-	public void setList(String key, List list) {
-		if(list!=null&&list.size()>0){
-			for (Object object : list) {
-				rightPushForList(key,object);
-			}
-		}
-	}
-
-    /**
-     * 写入map
-     * @param key
-     * @param map
-     */
-	public void setMap(String key, Map<String, Object> map) {
-		redisTemplate.opsForHash().putAll(key, map);
-	}
-
-    /**
-     * 获取map
-     * @param key
-     * @param mapKey
-     * @return
-     */
-	public Object get(String key, String mapKey) {
-		return redisTemplate.opsForHash().get(key, mapKey);
-	}
-
-    /**
-     * 写入map
-     * @param key
-     * @param hashKey
-     * @param hashValue
-     */
-	public void setMapByKV(String key, Object hashKey, Object hashValue) {
-		redisTemplate.opsForHash().put(key, hashKey,hashValue);
-	}
-
-    /**
-     * 删除map中的某个key-value
-     * @param key
-     * @param hashKey
-     */
-	public void removeHash(String key, String hashKey) {
-		redisTemplate.opsForHash().delete(key, hashKey);
-	}
-
-    /**
-     *
-     * @param key
-     * @return
-     */
-	public Map<Object, Object> getEntries(String key) {
-		return redisTemplate.opsForHash().entries(key);
-	}
-
-    /**
-     *
-     * @param key
-     * @param step
-     * @return
-     */
-	public long increase(String key, long step) {
-		return redisTemplate.opsForValue().increment(key, step);
-	}
-
-    /**
-     * 获取失效时间
-     * @param key
-     * @return
-     */
-	public long getExpireTime(String key) {
-		return redisTemplate.getExpire(key, TimeUnit.SECONDS);
-	}
-
-}

+ 17 - 11
src/main/java/com/caimei365/tools/task/StatisticsTask.java

@@ -1,12 +1,15 @@
 package com.caimei365.tools.task;
 
 import com.caimei365.tools.mapper.BaseMapper;
-import com.caimei365.tools.service.RedisService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
+import redis.clients.jedis.commands.JedisCommands;
 
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
@@ -28,7 +31,7 @@ public class StatisticsTask {
     @Resource
     private BaseMapper baseMapper;
     @Resource
-    private RedisService redisService;
+    private RedisTemplate<String,String> redisTemplate;
     /**
      * 每天凌晨3点执行一次统计前一天商品访问量
      * <p>
@@ -46,22 +49,25 @@ public class StatisticsTask {
             calendar.add(Calendar.DAY_OF_MONTH, -1);
             Date countTime = calendar.getTime();
             String dateStr = new SimpleDateFormat("yyyyMMdd").format(countTime);
-            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>【自动】统计前一天商品详情访问量,统计日期(前一天):" + dateStr);
+            log.info(">>>>>>>>>>>【定时任务】统计前一天商品详情访问量,统计日期(前一天):" + dateStr);
             // 获取Redis的Key: @Statistics(prefix = "statistics_details", field = "productId")
             String redisKey = "statistics_details:productId:" + dateStr;
-            Map<Object, Object> result = redisService.hgetall(redisKey);
-            for (Map.Entry<Object, Object> entry : result.entrySet()) {
-                if (null != entry) {
-                    Integer productId = (Integer) entry.getKey();
-                    Integer views = (Integer) entry.getValue();
-                    if (null != productId) {
+            Map<String, String> result = redisTemplate.execute(
+                    (RedisCallback<Map<String, String>>) connection -> (
+                            (JedisCommands) connection.getNativeConnection()
+                    ).hgetAll(redisKey)
+            );
+            if (null != result && !result.isEmpty()){
+                for (Map.Entry<String, String> entry : result.entrySet()) {
+                    if (null != entry && StringUtils.isNotEmpty(entry.getKey()) && StringUtils.isNotEmpty(entry.getValue())) {
+                        Integer productId = Integer.parseInt(entry.getKey());
+                        Integer views = Integer.parseInt(entry.getValue());
                         baseMapper.insertProductViews(productId, views, countTime);
                     }
                 }
             }
         } catch (Exception e) {
-            log.error("try-catch:",e);
-            log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>统计前一天商品详情访问量失败");
+            log.error(">>>>>>>>>>>【定时任务】统计前一天商品详情访问量失败:",e);
         }
     }
 }