Commit 091e4a2ec156b31601cb071913e98d39e4117add

Authored by guohuaizhi@taover.com
1 parent f83022cf
Exists in master

redis init

build.gradle
... ... @@ -36,7 +36,7 @@ dependencies {
36 36 "org.springframework.boot:spring-boot-starter-jetty",
37 37 "org.springframework.boot:spring-boot-starter-data-jpa",
38 38 "org.springframework.boot:spring-boot-starter-thymeleaf",
39   -
  39 + "org.springframework.boot:spring-boot-starter-data-redis",
40 40 "mysql:mysql-connector-java",
41 41  
42 42 "net.sf.json-lib:json-lib:2.2.3:jdk15",
... ...
resources/application-local.properties
1 1  
2   -server.port=81
  2 +server.port=8180
3 3 server.session.timeout=10
4 4  
5 5 spring.jackson.time-zone=GMT+8
... ... @@ -17,9 +17,9 @@ mail.smtp.auth=true
17 17 mail.smtp.sendFrom=taover-robot@taover.com
18 18  
19 19 spring.mvc.dispatch-options-request=true
20   -spring.datasource.url=jdbc\:mysql\://192.168.3.189\:3306/8zyun_ai?useUnicode\=true&characterEncoding\=UTF-8&autoReconnect\=true&zeroDateTimeBehavior\=convertToNull&transformedBitIsBoolean\=true
21   -spring.datasource.username=root
22   -spring.datasource.password=root
  20 +spring.datasource.url=jdbc\:mysql\://121.42.142.102\:3306/8zyun_ai?useUnicode\=true&characterEncoding\=UTF-8&autoReconnect\=true&zeroDateTimeBehavior\=convertToNull&transformedBitIsBoolean\=true
  21 +spring.datasource.username=dev
  22 +spring.datasource.password=taover02
23 23 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
24 24 spring.datasource.max-idle=10
25 25 spring.datasource.max-wait=10000
... ... @@ -37,3 +37,14 @@ accessKeyId=H4fIVB56iHjR6zQw
37 37 accessKeySecret=7bA395UltFp16kWPJT7Pfz0XYXCk4Q
38 38 endpoint=oss-cn-beijing.aliyuncs.com
39 39 bucket_contact=8zyun-oss
  40 +
  41 +spring.redis.host=127.0.0.1
  42 +spring.redis.database=0
  43 +spring.redis.port=6379
  44 +spring.redis.password=
  45 +spring.redis.pool.max-active=200
  46 +spring.redis.pool.max-wait=-1
  47 +spring.redis.pool.max-idle=10
  48 +spring.redis.pool.min-idle=0
  49 +spring.redis.timeout=1000
  50 +
... ...
src/main/java/com/taover/ai/StartUp.java
... ... @@ -2,6 +2,7 @@ package com.taover.ai;
2 2  
3 3 import org.apache.catalina.connector.Connector;
4 4 import org.springframework.boot.SpringApplication;
  5 +import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 7 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
7 8 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
... ... @@ -11,7 +12,10 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
11 12 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12 13 import org.springframework.web.util.UrlPathHelper;
13 14  
  15 +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
  16 +
14 17 @SpringBootApplication
  18 +@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class})
15 19 public class StartUp implements WebMvcConfigurer {
16 20 public static ConfigurableApplicationContext context;
17 21  
... ...
src/main/java/com/taover/ai/client/LocalClient.java
... ... @@ -7,8 +7,7 @@ import com.taover.util.UtilLog;
7 7 import net.sf.json.JSONObject;
8 8  
9 9 public class LocalClient {
10   - public static String localAnalysisUrl = "http://localhost:81/api/address/pcdDetail";
11   -
  10 + public static String localAnalysisUrl = "http://www.8zyun.com/address?address=";
12 11 /**
13 12 * 复用client,防止for循环调用内存溢出
14 13 * @param httpClient
... ... @@ -22,9 +21,11 @@ public class LocalClient {
22 21 content = content.replaceAll("\\r\\n", ";");
23 22 content = content.replaceAll("\\n", ";");
24 23  
  24 + String result = UtilHttpByOkHttp.sendGet(localAnalysisUrl + content, null);
  25 +
25 26 JSONObject data = new JSONObject();
26 27 data.put("address", content);
27   - return new ZtoAnalysisResponseData(UtilHttpByOkHttp.sendPostJson(localAnalysisUrl, null, data.toString()));
  28 + return new ZtoAnalysisResponseData(UtilHttpByOkHttp.sendGet(localAnalysisUrl + content, null) );
28 29 } catch (Exception e) {
29 30 UtilLog.errorForException(e, LocalClient.class);
30 31 throw new Exception("LOCAL接口请求解析失败");
... ...
src/main/java/com/taover/ai/common/AddressCommon.java
... ... @@ -5,6 +5,7 @@ import java.util.List;
5 5  
6 6 import javax.annotation.Resource;
7 7  
  8 +import org.springframework.jdbc.core.JdbcTemplate;
8 9 import org.springframework.stereotype.Service;
9 10  
10 11 import com.hankcs.hanlp.corpus.tag.Nature;
... ... @@ -23,6 +24,9 @@ public class AddressCommon {
23 24 @Resource
24 25 private AddressSegment addressSegment;
25 26  
  27 + @Resource
  28 + private JdbcTemplate jdbcTemplate;
  29 +
26 30 public BeanAddress pcdAddressOneAnalysis(String address){
27 31 List<Term> termList = AddressSegment.getAddressSegment().seg(address);
28 32 //CustomDictionary.add("七台河市", "ns");
... ... @@ -136,4 +140,12 @@ public class AddressCommon {
136 140 }
137 141 throw new Exception("识别失败");
138 142 }
  143 +
  144 + //查询订单里地址处理结果
  145 + private List<String>findOrderPdcResult(Integer fromOrderId,Integer toOrderId) {
  146 + //List list = this.jdbcTemplate.queryForObject("select count(*) from wxorder_excel_data where id!="+entity.getId()+" and file_id = '"+entity.getFileId()+"' and tenant_id ="+tenantId+" limit 1", Integer.class);
  147 + return null ;
  148 +
  149 +
  150 + }
139 151 }
... ...
src/main/java/com/taover/ai/config/RedisConfig.java 0 → 100644
... ... @@ -0,0 +1,117 @@
  1 +package com.taover.ai.config;
  2 +
  3 +
  4 +import com.fasterxml.jackson.annotation.JsonAutoDetect;
  5 +import com.fasterxml.jackson.annotation.PropertyAccessor;
  6 +import com.fasterxml.jackson.databind.ObjectMapper;
  7 +import org.springframework.cache.annotation.CachingConfigurerSupport;
  8 +import org.springframework.cache.annotation.EnableCaching;
  9 +import org.springframework.context.annotation.Bean;
  10 +import org.springframework.context.annotation.Configuration;
  11 +import org.springframework.data.redis.connection.RedisConnectionFactory;
  12 +import org.springframework.data.redis.core.*;
  13 +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  14 +import org.springframework.data.redis.serializer.StringRedisSerializer;
  15 +
  16 +/**
  17 + * redis配置类
  18 + * @program: springbootdemo
  19 + * @Date: 2020/12/22 09:20
  20 + * @Author: WZY
  21 + * @Description:
  22 + */
  23 +@Configuration
  24 +@EnableCaching //开启注解
  25 +public class RedisConfig extends CachingConfigurerSupport {
  26 +
  27 + /**
  28 + * retemplate相关配置
  29 + * @param factory
  30 + * @return
  31 + */
  32 + @Bean
  33 + public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  34 +
  35 + RedisTemplate<String, Object> template = new RedisTemplate<>();
  36 + // 配置连接工厂
  37 + template.setConnectionFactory(factory);
  38 +
  39 + //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
  40 + Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
  41 +
  42 + ObjectMapper om = new ObjectMapper();
  43 + // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
  44 + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  45 + // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
  46 + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  47 + jacksonSeial.setObjectMapper(om);
  48 +
  49 + // 值采用json序列化
  50 + template.setValueSerializer(jacksonSeial);
  51 + //使用StringRedisSerializer来序列化和反序列化redis的key值
  52 + template.setKeySerializer(new StringRedisSerializer());
  53 +
  54 + // 设置hash key 和value序列化模式
  55 + template.setHashKeySerializer(new StringRedisSerializer());
  56 + template.setHashValueSerializer(jacksonSeial);
  57 + template.afterPropertiesSet();
  58 +
  59 + return template;
  60 + }
  61 +
  62 + /**
  63 + * 对hash类型的数据操作
  64 + *
  65 + * @param redisTemplate
  66 + * @return
  67 + */
  68 + @Bean
  69 + public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
  70 + return redisTemplate.opsForHash();
  71 + }
  72 +
  73 + /**
  74 + * 对redis字符串类型数据操作
  75 + *
  76 + * @param redisTemplate
  77 + * @return
  78 + */
  79 + @Bean
  80 + public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
  81 + return redisTemplate.opsForValue();
  82 + }
  83 +
  84 + /**
  85 + * 对链表类型的数据操作
  86 + *
  87 + * @param redisTemplate
  88 + * @return
  89 + */
  90 + @Bean
  91 + public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
  92 + return redisTemplate.opsForList();
  93 + }
  94 +
  95 + /**
  96 + * 对无序集合类型的数据操作
  97 + *
  98 + * @param redisTemplate
  99 + * @return
  100 + */
  101 + @Bean
  102 + public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
  103 + return redisTemplate.opsForSet();
  104 + }
  105 +
  106 + /**
  107 + * 对有序集合类型的数据操作
  108 + *
  109 + * @param redisTemplate
  110 + * @return
  111 + */
  112 + @Bean
  113 + public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
  114 + return redisTemplate.opsForZSet();
  115 + }
  116 +
  117 +}
... ...
src/main/java/com/taover/ai/controller/api/DemoController.java
1 1 package com.taover.ai.controller.api;
2 2  
  3 +import javax.annotation.Resource;
3 4 import javax.servlet.http.HttpServletRequest;
4 5  
5 6 import org.springframework.web.bind.annotation.DeleteMapping;
... ... @@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.PutMapping;
9 10 import org.springframework.web.bind.annotation.RequestMapping;
10 11 import org.springframework.web.bind.annotation.RestController;
11 12  
  13 +import com.taover.ai.util.RedisUtil;
12 14 import com.taover.util.UtilLog;
13 15 import com.taover.util.bean.ResultInfo;
14 16 import com.taover.util.bean.UtilResultInfo;
... ... @@ -16,6 +18,10 @@ import com.taover.util.bean.UtilResultInfo;
16 18 @RestController("api.demo")
17 19 @RequestMapping("/api/demo")
18 20 public class DemoController {
  21 +
  22 + @Resource
  23 + private RedisUtil redisUtil;
  24 +
19 25 /**
20 26 * @apiDefine ResultInfo
21 27 * @apiSuccess {string}
... ... @@ -122,4 +128,16 @@ public class DemoController {
122 128 }
123 129 return UtilResultInfo.getSuccess("测试异常");
124 130 }
  131 +
  132 + @GetMapping(value="/testRedis")
  133 + public ResultInfo testRedis(){
  134 + try{
  135 + redisUtil.set("aaa", "test");
  136 + String aaa = (String) redisUtil.get("aaa");
  137 + System.out.print(aaa);
  138 + }catch(Exception e){
  139 + UtilLog.errorForException(e, this.getClass());
  140 + }
  141 + return UtilResultInfo.getSuccess("测试异常");
  142 + }
125 143 }
... ...
src/main/java/com/taover/ai/scaffold/InitExamineAddress.java
... ... @@ -27,11 +27,11 @@ import com.taover.ai.util.UtilMath;
27 27 import com.taover.util.UtilExcel;
28 28  
29 29 public class InitExamineAddress {
30   - public static String cursorPath = "D:\\workdata\\channel_upload_excel\\deal_cursor.txt";
31   - public static String dataDir = "D:\\workdata\\channel_upload_excel\\data";
32   - public static String detailAddressFile = "D:\\workdata\\channel_upload_excel\\detail_address.txt";
33   - public static String ztoCursorPath = "D:\\workdata\\channel_upload_excel\\zto_cursor.txt";
34   - public static String errorPath = "D:\\workdata\\channel_upload_excel\\error.txt";
  30 + public static String cursorPath = "C:\\work\\data\\channel_upload_excel\\channel_upload_excel\\data\\deal_cursor.txt";
  31 + public static String dataDir = "C:\\work\\data\\channel_upload_excel\\channel_upload_excel\\data";
  32 + public static String detailAddressFile = "C:\\work\\data\\channel_upload_excel\\channel_upload_excel\\detail_address.txt";
  33 + public static String ztoCursorPath = "C:\\work\\data\\channel_upload_excel\\channel_upload_excel\\zto_cursor.txt";
  34 + public static String errorPath = "C:\\work\\data\\channel_upload_excel\\channel_upload_excel\\error.txt";
35 35 public static JdbcTemplate template = getJdbcTemplate();
36 36  
37 37 public static void main(String[] args) {
... ... @@ -39,7 +39,7 @@ public class InitExamineAddress {
39 39  
40 40 //dealDataFile();
41 41  
42   - //ztoAnalysis();
  42 + ztoAnalysis();
43 43 }
44 44  
45 45 public static void ztoAnalysis() {
... ... @@ -47,21 +47,25 @@ public class InitExamineAddress {
47 47 //读取地址数据
48 48 List<String> detailList = readDetailAddress();
49 49 int startCursor = Integer.valueOf(readCursor(ztoCursorPath))+1;
50   - for(int i=startCursor; i<detailList.size(); ++i) {
  50 + for(int i=0; i<detailList.size(); ++i) {
51 51 String originAddress = detailList.get(i);
52 52 if(originAddress == null || originAddress.length() < 10) {
53 53 continue;
54 54 }
55 55  
56 56 try {
  57 +
  58 + //获取本地服务器解析结果
  59 + ZtoAnalysisResponseData localResponse = LocalClient.getAnalysisResult(originAddress);
  60 +
57 61 //获取中通解析结果
58 62 ZtoAnalysisResponseData ztoResponse = ZtoClient.getAnalysisResult(originAddress);
59 63  
60 64 //中通结果入库
61 65 insertZtoAnalysisRes(i, ztoResponse, originAddress);
62 66  
63   - //获取本地服务器解析结果
64   - ZtoAnalysisResponseData localResponse = LocalClient.getAnalysisResult(originAddress);
  67 +
  68 +
65 69  
66 70 //计算相似度
67 71 int[] score = calcScore(ztoResponse, localResponse);
... ...
src/main/java/com/taover/ai/service/AddressService.java
... ... @@ -9,4 +9,11 @@ public interface AddressService {
9 9 * @return
10 10 */
11 11 public ResponseAddress pcdAddress(String address);
  12 +
  13 + /**
  14 + * 刷新redis数据库
  15 + * @param address
  16 + * @return
  17 + */
  18 + public ResponseAddress freshRedisAddress();
12 19 }
... ...
src/main/java/com/taover/ai/service/impl/AddressServiceImpl.java
... ... @@ -26,4 +26,10 @@ public class AddressServiceImpl implements AddressService{
26 26 }
27 27 return ResponseAddress.createSuccess(beanList);
28 28 }
  29 +
  30 + @Override
  31 + public ResponseAddress freshRedisAddress() {
  32 + // TODO Auto-generated method stub
  33 + return null;
  34 + }
29 35 }
... ...
src/main/java/com/taover/ai/util/RedisUtil.java 0 → 100644
... ... @@ -0,0 +1,539 @@
  1 +package com.taover.ai.util;
  2 +
  3 +import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.data.redis.core.RedisTemplate;
  5 +import org.springframework.stereotype.Component;
  6 +import org.springframework.util.CollectionUtils;
  7 +
  8 +import java.util.List;
  9 +import java.util.Map;
  10 +import java.util.Set;
  11 +import java.util.concurrent.TimeUnit;
  12 +
  13 +/**
  14 + * redisTemplate封装
  15 + *
  16 + * @author zjjlive@dist.com.cn
  17 + */
  18 +@Component
  19 +public class RedisUtil {
  20 +
  21 + @Autowired
  22 + private RedisTemplate<String, Object> redisTemplate;
  23 +
  24 + public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
  25 + this.redisTemplate = redisTemplate;
  26 + }
  27 +
  28 + /**
  29 + * 指定缓存失效时间
  30 + * @param key 键
  31 + * @param time 时间(秒)
  32 + * @return
  33 + */
  34 + public boolean expire(String key,long time){
  35 + try {
  36 + if(time>0){
  37 + redisTemplate.expire(key, time, TimeUnit.SECONDS);
  38 + }
  39 + return true;
  40 + } catch (Exception e) {
  41 + e.printStackTrace();
  42 + return false;
  43 + }
  44 + }
  45 +
  46 + /**
  47 + * 根据key 获取过期时间
  48 + * @param key 键 不能为null
  49 + * @return 时间(秒) 返回0代表为永久有效
  50 + */
  51 + public long getExpire(String key){
  52 + return redisTemplate.getExpire(key,TimeUnit.SECONDS);
  53 + }
  54 +
  55 + /**
  56 + * 判断key是否存在
  57 + * @param key 键
  58 + * @return true 存在 false不存在
  59 + */
  60 + public boolean hasKey(String key){
  61 + try {
  62 + return redisTemplate.hasKey(key);
  63 + } catch (Exception e) {
  64 + e.printStackTrace();
  65 + return false;
  66 + }
  67 + }
  68 +
  69 + /**
  70 + * 删除缓存
  71 + * @param key 可以传一个值 或多个
  72 + */
  73 + @SuppressWarnings("unchecked")
  74 + public void del(String ... key){
  75 + if(key!=null&&key.length>0){
  76 + if(key.length==1){
  77 + redisTemplate.delete(key[0]);
  78 + }else{
  79 + redisTemplate.delete(CollectionUtils.arrayToList(key));
  80 + }
  81 + }
  82 + }
  83 +
  84 + //============================String=============================
  85 + /**
  86 + * 普通缓存获取
  87 + * @param key 键
  88 + * @return 值
  89 + */
  90 + public Object get(String key){
  91 + return key==null?null:redisTemplate.opsForValue().get(key);
  92 + }
  93 +
  94 + /**
  95 + * 普通缓存放入
  96 + * @param key 键
  97 + * @param value 值
  98 + * @return true成功 false失败
  99 + */
  100 + public boolean set(String key,Object value) {
  101 + try {
  102 + redisTemplate.opsForValue().set(key, value);
  103 + return true;
  104 + } catch (Exception e) {
  105 + e.printStackTrace();
  106 + return false;
  107 + }
  108 + }
  109 +
  110 + /**
  111 + * 普通缓存放入并设置时间
  112 + * @param key 键
  113 + * @param value 值
  114 + * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
  115 + * @return true成功 false 失败
  116 + */
  117 + public boolean set(String key,Object value,long time){
  118 + try {
  119 + if(time>0){
  120 + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  121 + }else{
  122 + set(key, value);
  123 + }
  124 + return true;
  125 + } catch (Exception e) {
  126 + e.printStackTrace();
  127 + return false;
  128 + }
  129 + }
  130 +
  131 + /**
  132 + * 递增
  133 + * @param key 键
  134 + * @param delta 要增加几(大于0)
  135 + * @return
  136 + */
  137 + public long incr(String key, long delta){
  138 + if(delta<0){
  139 + throw new RuntimeException("递增因子必须大于0");
  140 + }
  141 + return redisTemplate.opsForValue().increment(key, delta);
  142 + }
  143 +
  144 + /**
  145 + * 递减
  146 + * @param key 键
  147 + * @param delta 要减少几(小于0)
  148 + * @return
  149 + */
  150 + public long decr(String key, long delta){
  151 + if(delta<0){
  152 + throw new RuntimeException("递减因子必须大于0");
  153 + }
  154 + return redisTemplate.opsForValue().increment(key, -delta);
  155 + }
  156 +
  157 + //================================Map=================================
  158 + /**
  159 + * HashGet
  160 + * @param key 键 不能为null
  161 + * @param item 项 不能为null
  162 + * @return 值
  163 + */
  164 + public Object hget(String key,String item){
  165 + return redisTemplate.opsForHash().get(key, item);
  166 + }
  167 +
  168 + /**
  169 + * 获取hashKey对应的所有键值
  170 + * @param key 键
  171 + * @return 对应的多个键值
  172 + */
  173 + public Map<Object,Object> hmget(String key){
  174 + return redisTemplate.opsForHash().entries(key);
  175 + }
  176 +
  177 + /**
  178 + * HashSet
  179 + * @param key 键
  180 + * @param map 对应多个键值
  181 + * @return true 成功 false 失败
  182 + */
  183 + public boolean hmset(String key, Map<String,Object> map){
  184 + try {
  185 + redisTemplate.opsForHash().putAll(key, map);
  186 + return true;
  187 + } catch (Exception e) {
  188 + e.printStackTrace();
  189 + return false;
  190 + }
  191 + }
  192 +
  193 + /**
  194 + * HashSet 并设置时间
  195 + * @param key 键
  196 + * @param map 对应多个键值
  197 + * @param time 时间(秒)
  198 + * @return true成功 false失败
  199 + */
  200 + public boolean hmset(String key, Map<String,Object> map, long time){
  201 + try {
  202 + redisTemplate.opsForHash().putAll(key, map);
  203 + if(time>0){
  204 + expire(key, time);
  205 + }
  206 + return true;
  207 + } catch (Exception e) {
  208 + e.printStackTrace();
  209 + return false;
  210 + }
  211 + }
  212 +
  213 + /**
  214 + * 向一张hash表中放入数据,如果不存在将创建
  215 + * @param key 键
  216 + * @param item 项
  217 + * @param value 值
  218 + * @return true 成功 false失败
  219 + */
  220 + public boolean hset(String key,String item,Object value) {
  221 + try {
  222 + redisTemplate.opsForHash().put(key, item, value);
  223 + return true;
  224 + } catch (Exception e) {
  225 + e.printStackTrace();
  226 + return false;
  227 + }
  228 + }
  229 +
  230 + /**
  231 + * 向一张hash表中放入数据,如果不存在将创建
  232 + * @param key 键
  233 + * @param item 项
  234 + * @param value 值
  235 + * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
  236 + * @return true 成功 false失败
  237 + */
  238 + public boolean hset(String key,String item,Object value,long time) {
  239 + try {
  240 + redisTemplate.opsForHash().put(key, item, value);
  241 + if(time>0){
  242 + expire(key, time);
  243 + }
  244 + return true;
  245 + } catch (Exception e) {
  246 + e.printStackTrace();
  247 + return false;
  248 + }
  249 + }
  250 +
  251 + /**
  252 + * 删除hash表中的值
  253 + * @param key 键 不能为null
  254 + * @param item 项 可以使多个 不能为null
  255 + */
  256 + public void hdel(String key, Object... item){
  257 + redisTemplate.opsForHash().delete(key,item);
  258 + }
  259 +
  260 + /**
  261 + * 判断hash表中是否有该项的值
  262 + * @param key 键 不能为null
  263 + * @param item 项 不能为null
  264 + * @return true 存在 false不存在
  265 + */
  266 + public boolean hHasKey(String key, String item){
  267 + return redisTemplate.opsForHash().hasKey(key, item);
  268 + }
  269 +
  270 + /**
  271 + * hash递增 如果不存在,就会创建一个 并把新增后的值返回
  272 + * @param key 键
  273 + * @param item 项
  274 + * @param by 要增加几(大于0)
  275 + * @return
  276 + */
  277 + public double hincr(String key, String item,double by){
  278 + return redisTemplate.opsForHash().increment(key, item, by);
  279 + }
  280 +
  281 + /**
  282 + * hash递减
  283 + * @param key 键
  284 + * @param item 项
  285 + * @param by 要减少记(小于0)
  286 + * @return
  287 + */
  288 + public double hdecr(String key, String item,double by){
  289 + return redisTemplate.opsForHash().increment(key, item,-by);
  290 + }
  291 +
  292 + //============================set=============================
  293 + /**
  294 + * 根据key获取Set中的所有值
  295 + * @param key 键
  296 + * @return
  297 + */
  298 + public Set<Object> sGet(String key){
  299 + try {
  300 + return redisTemplate.opsForSet().members(key);
  301 + } catch (Exception e) {
  302 + e.printStackTrace();
  303 + return null;
  304 + }
  305 + }
  306 +
  307 + /**
  308 + * 根据value从一个set中查询,是否存在
  309 + * @param key 键
  310 + * @param value 值
  311 + * @return true 存在 false不存在
  312 + */
  313 + public boolean sHasKey(String key,Object value){
  314 + try {
  315 + return redisTemplate.opsForSet().isMember(key, value);
  316 + } catch (Exception e) {
  317 + e.printStackTrace();
  318 + return false;
  319 + }
  320 + }
  321 +
  322 + /**
  323 + * 将数据放入set缓存
  324 + * @param key 键
  325 + * @param values 值 可以是多个
  326 + * @return 成功个数
  327 + */
  328 + public long sSet(String key, Object...values) {
  329 + try {
  330 + return redisTemplate.opsForSet().add(key, values);
  331 + } catch (Exception e) {
  332 + e.printStackTrace();
  333 + return 0;
  334 + }
  335 + }
  336 +
  337 + /**
  338 + * 将set数据放入缓存
  339 + * @param key 键
  340 + * @param time 时间(秒)
  341 + * @param values 值 可以是多个
  342 + * @return 成功个数
  343 + */
  344 + public long sSetAndTime(String key,long time,Object...values) {
  345 + try {
  346 + Long count = redisTemplate.opsForSet().add(key, values);
  347 + if(time>0) {
  348 + expire(key, time);
  349 + }
  350 + return count;
  351 + } catch (Exception e) {
  352 + e.printStackTrace();
  353 + return 0;
  354 + }
  355 + }
  356 +
  357 + /**
  358 + * 获取set缓存的长度
  359 + * @param key 键
  360 + * @return
  361 + */
  362 + public long sGetSetSize(String key){
  363 + try {
  364 + return redisTemplate.opsForSet().size(key);
  365 + } catch (Exception e) {
  366 + e.printStackTrace();
  367 + return 0;
  368 + }
  369 + }
  370 +
  371 + /**
  372 + * 移除值为value的
  373 + * @param key 键
  374 + * @param values 值 可以是多个
  375 + * @return 移除的个数
  376 + */
  377 + public long setRemove(String key, Object ...values) {
  378 + try {
  379 + Long count = redisTemplate.opsForSet().remove(key, values);
  380 + return count;
  381 + } catch (Exception e) {
  382 + e.printStackTrace();
  383 + return 0;
  384 + }
  385 + }
  386 + //===============================list=================================
  387 +
  388 + /**
  389 + * 获取list缓存的内容
  390 + * @param key 键
  391 + * @param start 开始
  392 + * @param end 结束 0 到 -1代表所有值
  393 + * @return
  394 + */
  395 + public List<Object> lGet(String key, long start, long end){
  396 + try {
  397 + return redisTemplate.opsForList().range(key, start, end);
  398 + } catch (Exception e) {
  399 + e.printStackTrace();
  400 + return null;
  401 + }
  402 + }
  403 +
  404 + /**
  405 + * 获取list缓存的长度
  406 + * @param key 键
  407 + * @return
  408 + */
  409 + public long lGetListSize(String key){
  410 + try {
  411 + return redisTemplate.opsForList().size(key);
  412 + } catch (Exception e) {
  413 + e.printStackTrace();
  414 + return 0;
  415 + }
  416 + }
  417 +
  418 + /**
  419 + * 通过索引 获取list中的值
  420 + * @param key 键
  421 + * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
  422 + * @return
  423 + */
  424 + public Object lGetIndex(String key,long index){
  425 + try {
  426 + return redisTemplate.opsForList().index(key, index);
  427 + } catch (Exception e) {
  428 + e.printStackTrace();
  429 + return null;
  430 + }
  431 + }
  432 +
  433 + /**
  434 + * 将list放入缓存
  435 + * @param key 键
  436 + * @param value 值
  437 + * @return
  438 + */
  439 + public boolean lSet(String key, Object value) {
  440 + try {
  441 + redisTemplate.opsForList().rightPush(key, value);
  442 + return true;
  443 + } catch (Exception e) {
  444 + e.printStackTrace();
  445 + return false;
  446 + }
  447 + }
  448 +
  449 + /**
  450 + * 将list放入缓存
  451 + * @param key 键
  452 + * @param value 值
  453 + * @param time 时间(秒)
  454 + * @return
  455 + */
  456 + public boolean lSet(String key, Object value, long time) {
  457 + try {
  458 + redisTemplate.opsForList().rightPush(key, value);
  459 + if (time > 0) {
  460 + expire(key, time);
  461 + }
  462 + return true;
  463 + } catch (Exception e) {
  464 + e.printStackTrace();
  465 + return false;
  466 + }
  467 + }
  468 +
  469 + /**
  470 + * 将list放入缓存
  471 + * @param key 键
  472 + * @param value 值
  473 + * @return
  474 + */
  475 + public boolean lSet(String key, List<Object> value) {
  476 + try {
  477 + redisTemplate.opsForList().rightPushAll(key, value);
  478 + return true;
  479 + } catch (Exception e) {
  480 + e.printStackTrace();
  481 + return false;
  482 + }
  483 + }
  484 +
  485 + /**
  486 + * 将list放入缓存
  487 + * @param key 键
  488 + * @param value 值
  489 + * @param time 时间(秒)
  490 + * @return
  491 + */
  492 + public boolean lSet(String key, List<Object> value, long time) {
  493 + try {
  494 + redisTemplate.opsForList().rightPushAll(key, value);
  495 + if (time > 0) {
  496 + expire(key, time);
  497 + }
  498 + return true;
  499 + } catch (Exception e) {
  500 + e.printStackTrace();
  501 + return false;
  502 + }
  503 + }
  504 +
  505 + /**
  506 + * 根据索引修改list中的某条数据
  507 + * @param key 键
  508 + * @param index 索引
  509 + * @param value 值
  510 + * @return
  511 + */
  512 + public boolean lUpdateIndex(String key, long index,Object value) {
  513 + try {
  514 + redisTemplate.opsForList().set(key, index, value);
  515 + return true;
  516 + } catch (Exception e) {
  517 + e.printStackTrace();
  518 + return false;
  519 + }
  520 + }
  521 +
  522 + /**
  523 + * 移除N个值为value
  524 + * @param key 键
  525 + * @param count 移除多少个
  526 + * @param value 值
  527 + * @return 移除的个数
  528 + */
  529 + public long lRemove(String key,long count,Object value) {
  530 + try {
  531 + Long remove = redisTemplate.opsForList().remove(key, count, value);
  532 + return remove;
  533 + } catch (Exception e) {
  534 + e.printStackTrace();
  535 + return 0;
  536 + }
  537 + }
  538 +
  539 +}
... ...