• springboot 整合redis ,jedis


    SpringBoot2.0笔记

    (一)SpringBoot基本操作——环境搭建及项目创建(有demo)

    (二)SpringBoot基本操作——使用IDEA打war包发布及测试

    (三)SpringBoot基本操作——SpringBoot整合SpringDataJpa(有demo)

    (四)SpringBoot基本操作——SpringBoot使用RedisTemplate整合Redis(有demo)

    (五)SpringBoot基本操作——SpringBoot使用Jedis整合Redis(有demo)

    (六)SpringBoot基本操作——SpringBoot使用Junit4单元测试(有demo)

    (七)SpringBoot基本操作——SpringBoot整合Shiro权限管理(完整demo+界面)

    本篇基于Springboot2.0 + Redis实现数据缓存及分库存储,上一篇主要使用官方推荐的RedisTemplate实现,这篇我主要以Jedis的方式来实现Redis的操作。这里再重复一遍,两种方式都可以实现对Redis的操作,RedisTemplate是对Jedis做了封装,官方推荐使用Redistemplate方式。

    本文使用idea工具构建Springboot+SpringMvc+Thymeleaf+SpringDataJPA+MySql+Redis项目

    GitHub地址:https://github.com/jwwam/springbootRedis2.git

    一、Redis基本配置及工具类

    还是来样子先看下项目demo的目录结构,基本的目录结构和上篇没什么差别。文末会给出pom包配置。

    1.创建redis.properties配置文件

    1. #redis配置开始
    2. # Redis数据库索引(默认为0)
    3. spring.redis.database=0
    4. # Redis服务器地址
    5. spring.redis.host=localhost
    6. # Redis服务器连接端口
    7. spring.redis.port=6379
    8. # Redis服务器连接密码(默认为空)
    9. spring.redis.password=123456
    10. # 连接池最大连接数(使用负值表示没有限制)
    11. spring.redis.jedis.pool.max-active=1024
    12. # 连接池最大阻塞等待时间(使用负值表示没有限制)
    13. spring.redis.jedis.pool.max-wait=10000
    14. # 连接池中的最大空闲连接
    15. spring.redis.jedis.pool.max-idle=200
    16. # 连接池中的最小空闲连接
    17. spring.redis.jedis.pool.min-idle=0
    18. # 连接超时时间(毫秒)
    19. spring.redis.timeout=10000
    20. #redis配置结束
    21. spring.redis.block-when-exhausted=true

    这里我们要注意max-active,max-wait,max-idle,min-idle这几个参数版本不同写法也不一样,我这里由于引入了最新的Jedis包所以写法如上,请注意。

    2.添加配置类RedisConfig.java

    1. package com.springboot.demo.base.config;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import org.springframework.context.annotation.PropertySource;
    7. import redis.clients.jedis.JedisPool;
    8. import redis.clients.jedis.JedisPoolConfig;
    9. @Configuration
    10. @PropertySource("classpath:redis.properties")
    11. @Slf4j
    12. public class RedisConfig {
    13. @Value("${spring.redis.host}")
    14. private String host;
    15. @Value("${spring.redis.port}")
    16. private int port;
    17. @Value("${spring.redis.timeout}")
    18. private int timeout;
    19. @Value("${spring.redis.jedis.pool.max-idle}")
    20. private int maxIdle;
    21. @Value("${spring.redis.jedis.pool.max-wait}")
    22. private long maxWaitMillis;
    23. @Value("${spring.redis.password}")
    24. private String password;
    25. @Value("${spring.redis.block-when-exhausted}")
    26. private boolean blockWhenExhausted;
    27. @Bean
    28. public JedisPool redisPoolFactory() throws Exception{
    29. log.info("JedisPool注入成功!!");
    30. log.info("redis地址:" + host + ":" + port);
    31. JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    32. jedisPoolConfig.setMaxIdle(maxIdle);
    33. jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    34. // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
    35. jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
    36. // 是否启用pool的jmx管理功能, 默认true
    37. jedisPoolConfig.setJmxEnabled(true);
    38. JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    39. return jedisPool;
    40. }
    41. }

    3.创建RedisUtil.java工具类,注意,这是一个非常完整的Jedis操作redis的工具类,里面多数方法我都加入了分库操作,使用时注意规范就好。

    1. package com.springboot.demo.base.utils;
    2. import java.io.ByteArrayInputStream;
    3. import java.io.ByteArrayOutputStream;
    4. import java.io.ObjectInputStream;
    5. import java.io.ObjectOutputStream;
    6. import java.util.List;
    7. import java.util.Map;
    8. import java.util.Set;
    9. import lombok.extern.slf4j.Slf4j;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. import org.springframework.stereotype.Component;
    12. import redis.clients.jedis.BinaryClient.LIST_POSITION;
    13. import redis.clients.jedis.Jedis;
    14. import redis.clients.jedis.JedisPool;
    15. import redis.clients.jedis.SortingParams;
    16. @Component
    17. @Slf4j
    18. public class RedisUtil{
    19. @Autowired
    20. private JedisPool jedisPool;
    21. /**
    22. * <p>
    23. * 通过key获取储存在redis中的value
    24. * </p>
    25. * <p>
    26. * 并释放连接
    27. * </p>
    28. *
    29. * @param key
    30. * @param indexdb 选择redis库 0-15
    31. * @return 成功返回value 失败返回null
    32. */
    33. public String get(String key,int indexdb) {
    34. Jedis jedis = null;
    35. String value = null;
    36. try {
    37. jedis = jedisPool.getResource();
    38. jedis.select(indexdb);
    39. value = jedis.get(key);
    40. log.info(value);
    41. } catch (Exception e) {
    42. log.error(e.getMessage());
    43. } finally {
    44. returnResource(jedisPool, jedis);
    45. }
    46. return value;
    47. }
    48. /**
    49. * <p>
    50. * 通过key获取储存在redis中的value
    51. * </p>
    52. * <p>
    53. * 并释放连接
    54. * </p>
    55. *
    56. * @param key
    57. * @param indexdb 选择redis库 0-15
    58. * @return 成功返回value 失败返回null
    59. */
    60. public byte[] get(byte[] key,int indexdb) {
    61. Jedis jedis = null;
    62. byte[] value = null;
    63. try {
    64. jedis = jedisPool.getResource();
    65. jedis.select(indexdb);
    66. value = jedis.get(key);
    67. } catch (Exception e) {
    68. log.error(e.getMessage());
    69. } finally {
    70. returnResource(jedisPool, jedis);
    71. }
    72. return value;
    73. }
    74. /**
    75. * <p>
    76. * 向redis存入key和value,并释放连接资源
    77. * </p>
    78. * <p>
    79. * 如果key已经存在 则覆盖
    80. * </p>
    81. *
    82. * @param key
    83. * @param value
    84. * @param indexdb 选择redis库 0-15
    85. * @return 成功 返回OK 失败返回 0
    86. */
    87. public String set(String key, String value,int indexdb) {
    88. Jedis jedis = null;
    89. try {
    90. jedis = jedisPool.getResource();
    91. jedis.select(indexdb);
    92. return jedis.set(key, value);
    93. } catch (Exception e) {
    94. log.error(e.getMessage());
    95. return "0";
    96. } finally {
    97. returnResource(jedisPool, jedis);
    98. }
    99. }
    100. /**
    101. * <p>
    102. * 向redis存入key和value,并释放连接资源
    103. * </p>
    104. * <p>
    105. * 如果key已经存在 则覆盖
    106. * </p>
    107. *
    108. * @param key
    109. * @param value
    110. * @param indexdb 选择redis库 0-15
    111. * @return 成功 返回OK 失败返回 0
    112. */
    113. public String set(byte[] key, byte[] value,int indexdb) {
    114. Jedis jedis = null;
    115. try {
    116. jedis = jedisPool.getResource();
    117. jedis.select(indexdb);
    118. return jedis.set(key, value);
    119. } catch (Exception e) {
    120. log.error(e.getMessage());
    121. return "0";
    122. } finally {
    123. returnResource(jedisPool, jedis);
    124. }
    125. }
    126. /**
    127. * <p>
    128. * 删除指定的key,也可以传入一个包含key的数组
    129. * </p>
    130. *
    131. * @param keys 一个key 也可以使 string 数组
    132. * @return 返回删除成功的个数
    133. */
    134. public Long del(String... keys) {
    135. Jedis jedis = null;
    136. try {
    137. jedis = jedisPool.getResource();
    138. return jedis.del(keys);
    139. } catch (Exception e) {
    140. log.error(e.getMessage());
    141. return 0L;
    142. } finally {
    143. returnResource(jedisPool, jedis);
    144. }
    145. }
    146. /**
    147. * <p>
    148. * 删除指定的key,也可以传入一个包含key的数组
    149. * </p>
    150. * @param indexdb 选择redis库 0-15
    151. * @param keys 一个key 也可以使 string 数组
    152. * @return 返回删除成功的个数
    153. */
    154. public Long del(int indexdb,String... keys) {
    155. Jedis jedis = null;
    156. try {
    157. jedis = jedisPool.getResource();
    158. jedis.select(indexdb);
    159. return jedis.del(keys);
    160. } catch (Exception e) {
    161. log.error(e.getMessage());
    162. return 0L;
    163. } finally {
    164. returnResource(jedisPool, jedis);
    165. }
    166. }
    167. /**
    168. * <p>
    169. * 删除指定的key,也可以传入一个包含key的数组
    170. * </p>
    171. * @param indexdb 选择redis库 0-15
    172. * @param keys 一个key 也可以使 string 数组
    173. * @return 返回删除成功的个数
    174. */
    175. public Long del(int indexdb,byte[]... keys) {
    176. Jedis jedis = null;
    177. try {
    178. jedis = jedisPool.getResource();
    179. jedis.select(indexdb);
    180. return jedis.del(keys);
    181. } catch (Exception e) {
    182. log.error(e.getMessage());
    183. return 0L;
    184. } finally {
    185. returnResource(jedisPool, jedis);
    186. }
    187. }
    188. /**
    189. * <p>
    190. * 通过key向指定的value值追加值
    191. * </p>
    192. *
    193. * @param key
    194. * @param str
    195. * @return 成功返回 添加后value的长度 失败 返回 添加的 value 的长度 异常返回0L
    196. */
    197. public Long append(String key, String str) {
    198. Jedis jedis = null;
    199. Long res = null;
    200. try {
    201. jedis = jedisPool.getResource();
    202. res = jedis.append(key, str);
    203. } catch (Exception e) {
    204. log.error(e.getMessage());
    205. return 0L;
    206. } finally {
    207. returnResource(jedisPool, jedis);
    208. }
    209. return res;
    210. }
    211. /**
    212. * <p>
    213. * 判断key是否存在
    214. * </p>
    215. *
    216. * @param key
    217. * @return true OR false
    218. */
    219. public Boolean exists(String key) {
    220. Jedis jedis = null;
    221. try {
    222. jedis = jedisPool.getResource();
    223. return jedis.exists(key);
    224. } catch (Exception e) {
    225. log.error(e.getMessage());
    226. return false;
    227. } finally {
    228. returnResource(jedisPool, jedis);
    229. }
    230. }
    231. /**
    232. * <p>
    233. * 清空当前数据库中的所有 key,此命令从不失败。
    234. * </p>
    235. *
    236. * @return 总是返回 OK
    237. */
    238. public String flushDB() {
    239. Jedis jedis = null;
    240. try {
    241. jedis = jedisPool.getResource();
    242. return jedis.flushDB();
    243. } catch (Exception e) {
    244. log.error(e.getMessage());
    245. } finally {
    246. returnResource(jedisPool, jedis);
    247. }
    248. return null;
    249. }
    250. /**
    251. * <p>
    252. * 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
    253. * </p>
    254. *
    255. * @param key
    256. * @param value
    257. * 过期时间,单位:秒
    258. * @return 成功返回1 如果存在 和 发生异常 返回 0
    259. */
    260. public Long expire(String key, int value, int indexdb) {
    261. Jedis jedis = null;
    262. try {
    263. jedis = jedisPool.getResource();
    264. jedis.select(indexdb);
    265. return jedis.expire(key, value);
    266. } catch (Exception e) {
    267. log.error(e.getMessage());
    268. return 0L;
    269. } finally {
    270. returnResource(jedisPool, jedis);
    271. }
    272. }
    273. /**
    274. * <p>
    275. * 以秒为单位,返回给定 key 的剩余生存时间
    276. * </p>
    277. *
    278. * @param key
    279. * @return 当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以秒为单位,返回 key
    280. * 的剩余生存时间。 发生异常 返回 0
    281. */
    282. public Long ttl(String key,int indexdb) {
    283. Jedis jedis = null;
    284. try {
    285. jedis = jedisPool.getResource();
    286. jedis.select(indexdb);
    287. return jedis.ttl(key);
    288. } catch (Exception e) {
    289. log.error(e.getMessage());
    290. return 0L;
    291. } finally {
    292. returnResource(jedisPool, jedis);
    293. }
    294. }
    295. /**
    296. * <p>
    297. * 移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )
    298. * </p>
    299. *
    300. * @param key
    301. * @return 当生存时间移除成功时,返回 1 .如果 key 不存在或 key 没有设置生存时间,返回 0 , 发生异常 返回 -1
    302. */
    303. public Long persist(String key) {
    304. Jedis jedis = null;
    305. try {
    306. jedis = jedisPool.getResource();
    307. return jedis.persist(key);
    308. } catch (Exception e) {
    309. log.error(e.getMessage());
    310. return -1L;
    311. } finally {
    312. returnResource(jedisPool, jedis);
    313. }
    314. }
    315. /**
    316. * <p>
    317. * 新增key,并将 key 的生存时间 (以秒为单位)
    318. * </p>
    319. *
    320. * @param key
    321. * @param seconds
    322. * 生存时间 单位:秒
    323. * @param value
    324. * @return 设置成功时返回 OK 。当 seconds 参数不合法时,返回一个错误。
    325. */
    326. public String setex(String key, int seconds, String value) {
    327. Jedis jedis = null;
    328. try {
    329. jedis = jedisPool.getResource();
    330. return jedis.setex(key, seconds, value);
    331. } catch (Exception e) {
    332. log.error(e.getMessage());
    333. } finally {
    334. returnResource(jedisPool, jedis);
    335. }
    336. return null;
    337. }
    338. /**
    339. * <p>
    340. * 设置key value,如果key已经存在则返回0,nx==> not exist
    341. * </p>
    342. *
    343. * @param key
    344. * @param value
    345. * @return 成功返回1 如果存在 和 发生异常 返回 0
    346. */
    347. public Long setnx(String key, String value) {
    348. Jedis jedis = null;
    349. try {
    350. jedis = jedisPool.getResource();
    351. return jedis.setnx(key, value);
    352. } catch (Exception e) {
    353. log.error(e.getMessage());
    354. return 0L;
    355. } finally {
    356. returnResource(jedisPool, jedis);
    357. }
    358. }
    359. /**
    360. * <p>
    361. * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
    362. * </p>
    363. * <p>
    364. * 当 key 存在但不是字符串类型时,返回一个错误。
    365. * </p>
    366. *
    367. * @param key
    368. * @param value
    369. * @return 返回给定 key 的旧值。当 key 没有旧值时,也即是, key 不存在时,返回 nil
    370. */
    371. public String getSet(String key, String value) {
    372. Jedis jedis = null;
    373. try {
    374. jedis = jedisPool.getResource();
    375. return jedis.getSet(key, value);
    376. } catch (Exception e) {
    377. log.error(e.getMessage());
    378. } finally {
    379. returnResource(jedisPool, jedis);
    380. }
    381. return null;
    382. }
    383. /**
    384. * <p>
    385. * 设置key value并制定这个键值的有效期
    386. * </p>
    387. *
    388. * @param key
    389. * @param value
    390. * @param seconds
    391. * 单位:秒
    392. * @return 成功返回OK 失败和异常返回null
    393. */
    394. public String setex(String key, String value, int seconds) {
    395. Jedis jedis = null;
    396. String res = null;
    397. try {
    398. jedis = jedisPool.getResource();
    399. res = jedis.setex(key, seconds, value);
    400. } catch (Exception e) {
    401. log.error(e.getMessage());
    402. } finally {
    403. returnResource(jedisPool, jedis);
    404. }
    405. return res;
    406. }
    407. /**
    408. * <p>
    409. * 通过key 和offset 从指定的位置开始将原先value替换
    410. * </p>
    411. * <p>
    412. * 下标从0开始,offset表示从offset下标开始替换
    413. * </p>
    414. * <p>
    415. * 如果替换的字符串长度过小则会这样
    416. * </p>
    417. * <p>
    418. * example:
    419. * </p>
    420. * <p>
    421. * value : bigsea@zto.cn
    422. * </p>
    423. * <p>
    424. * str : abc
    425. * </p>
    426. * <P>
    427. * 从下标7开始替换 则结果为
    428. * </p>
    429. * <p>
    430. * RES : bigsea.abc.cn
    431. * </p>
    432. *
    433. * @param key
    434. * @param str
    435. * @param offset
    436. * 下标位置
    437. * @return 返回替换后 value 的长度
    438. */
    439. public Long setrange(String key, String str, int offset) {
    440. Jedis jedis = null;
    441. try {
    442. jedis = jedisPool.getResource();
    443. return jedis.setrange(key, offset, str);
    444. } catch (Exception e) {
    445. log.error(e.getMessage());
    446. return 0L;
    447. } finally {
    448. returnResource(jedisPool, jedis);
    449. }
    450. }
    451. /**
    452. * <p>
    453. * 通过批量的key获取批量的value
    454. * </p>
    455. *
    456. * @param keys
    457. * string数组 也可以是一个key
    458. * @return 成功返回value的集合, 失败返回null的集合 ,异常返回空
    459. */
    460. public List<String> mget(String... keys) {
    461. Jedis jedis = null;
    462. List<String> values = null;
    463. try {
    464. jedis = jedisPool.getResource();
    465. values = jedis.mget(keys);
    466. } catch (Exception e) {
    467. log.error(e.getMessage());
    468. } finally {
    469. returnResource(jedisPool, jedis);
    470. }
    471. return values;
    472. }
    473. /**
    474. * <p>
    475. * 批量的设置key:value,可以一个
    476. * </p>
    477. * <p>
    478. * example:
    479. * </p>
    480. * <p>
    481. * obj.mset(new String[]{"key2","value1","key2","value2"})
    482. * </p>
    483. *
    484. * @param keysvalues
    485. * @return 成功返回OK 失败 异常 返回 null
    486. *
    487. */
    488. public String mset(String... keysvalues) {
    489. Jedis jedis = null;
    490. String res = null;
    491. try {
    492. jedis = jedisPool.getResource();
    493. res = jedis.mset(keysvalues);
    494. } catch (Exception e) {
    495. log.error(e.getMessage());
    496. } finally {
    497. returnResource(jedisPool, jedis);
    498. }
    499. return res;
    500. }
    501. /**
    502. * <p>
    503. * 批量的设置key:value,可以一个,如果key已经存在则会失败,操作会回滚
    504. * </p>
    505. * <p>
    506. * example:
    507. * </p>
    508. * <p>
    509. * obj.msetnx(new String[]{"key2","value1","key2","value2"})
    510. * </p>
    511. *
    512. * @param keysvalues
    513. * @return 成功返回1 失败返回0
    514. */
    515. public Long msetnx(String... keysvalues) {
    516. Jedis jedis = null;
    517. Long res = 0L;
    518. try {
    519. jedis = jedisPool.getResource();
    520. res = jedis.msetnx(keysvalues);
    521. } catch (Exception e) {
    522. log.error(e.getMessage());
    523. } finally {
    524. returnResource(jedisPool, jedis);
    525. }
    526. return res;
    527. }
    528. /**
    529. * <p>
    530. * 设置key的值,并返回一个旧值
    531. * </p>
    532. *
    533. * @param key
    534. * @param value
    535. * @return 旧值 如果key不存在 则返回null
    536. */
    537. public String getset(String key, String value) {
    538. Jedis jedis = null;
    539. String res = null;
    540. try {
    541. jedis = jedisPool.getResource();
    542. res = jedis.getSet(key, value);
    543. } catch (Exception e) {
    544. log.error(e.getMessage());
    545. } finally {
    546. returnResource(jedisPool, jedis);
    547. }
    548. return res;
    549. }
    550. /**
    551. * <p>
    552. * 通过下标 和key 获取指定下标位置的 value
    553. * </p>
    554. *
    555. * @param key
    556. * @param startOffset
    557. * 开始位置 从0 开始 负数表示从右边开始截取
    558. * @param endOffset
    559. * @return 如果没有返回null
    560. */
    561. public String getrange(String key, int startOffset, int endOffset) {
    562. Jedis jedis = null;
    563. String res = null;
    564. try {
    565. jedis = jedisPool.getResource();
    566. res = jedis.getrange(key, startOffset, endOffset);
    567. } catch (Exception e) {
    568. log.error(e.getMessage());
    569. } finally {
    570. returnResource(jedisPool, jedis);
    571. }
    572. return res;
    573. }
    574. /**
    575. * <p>
    576. * 通过key 对value进行加值+1操作,当value不是int类型时会返回错误,当key不存在是则value为1
    577. * </p>
    578. *
    579. * @param key
    580. * @return 加值后的结果
    581. */
    582. public Long incr(String key) {
    583. Jedis jedis = null;
    584. Long res = null;
    585. try {
    586. jedis = jedisPool.getResource();
    587. res = jedis.incr(key);
    588. } catch (Exception e) {
    589. log.error(e.getMessage());
    590. } finally {
    591. returnResource(jedisPool, jedis);
    592. }
    593. return res;
    594. }
    595. /**
    596. * <p>
    597. * 通过key给指定的value加值,如果key不存在,则这是value为该值
    598. * </p>
    599. *
    600. * @param key
    601. * @param integer
    602. * @return
    603. */
    604. public Long incrBy(String key, Long integer) {
    605. Jedis jedis = null;
    606. Long res = null;
    607. try {
    608. jedis = jedisPool.getResource();
    609. res = jedis.incrBy(key, integer);
    610. } catch (Exception e) {
    611. log.error(e.getMessage());
    612. } finally {
    613. returnResource(jedisPool, jedis);
    614. }
    615. return res;
    616. }
    617. /**
    618. * <p>
    619. * 对key的值做减减操作,如果key不存在,则设置key为-1
    620. * </p>
    621. *
    622. * @param key
    623. * @return
    624. */
    625. public Long decr(String key) {
    626. Jedis jedis = null;
    627. Long res = null;
    628. try {
    629. jedis = jedisPool.getResource();
    630. res = jedis.decr(key);
    631. } catch (Exception e) {
    632. log.error(e.getMessage());
    633. } finally {
    634. returnResource(jedisPool, jedis);
    635. }
    636. return res;
    637. }
    638. /**
    639. * <p>
    640. * 减去指定的值
    641. * </p>
    642. *
    643. * @param key
    644. * @param integer
    645. * @return
    646. */
    647. public Long decrBy(String key, Long integer) {
    648. Jedis jedis = null;
    649. Long res = null;
    650. try {
    651. jedis = jedisPool.getResource();
    652. res = jedis.decrBy(key, integer);
    653. } catch (Exception e) {
    654. log.error(e.getMessage());
    655. } finally {
    656. returnResource(jedisPool, jedis);
    657. }
    658. return res;
    659. }
    660. /**
    661. * <p>
    662. * 通过key获取value值的长度
    663. * </p>
    664. *
    665. * @param key
    666. * @return 失败返回null
    667. */
    668. public Long serlen(String key) {
    669. Jedis jedis = null;
    670. Long res = null;
    671. try {
    672. jedis = jedisPool.getResource();
    673. res = jedis.strlen(key);
    674. } catch (Exception e) {
    675. log.error(e.getMessage());
    676. } finally {
    677. returnResource(jedisPool, jedis);
    678. }
    679. return res;
    680. }
    681. /**
    682. * <p>
    683. * 通过key给field设置指定的值,如果key不存在,则先创建
    684. * </p>
    685. *
    686. * @param key
    687. * @param field
    688. * 字段
    689. * @param value
    690. * @return 如果存在返回0 异常返回null
    691. */
    692. public Long hset(String key, String field, String value) {
    693. Jedis jedis = null;
    694. Long res = null;
    695. try {
    696. jedis = jedisPool.getResource();
    697. res = jedis.hset(key, field, value);
    698. } catch (Exception e) {
    699. log.error(e.getMessage());
    700. } finally {
    701. returnResource(jedisPool, jedis);
    702. }
    703. return res;
    704. }
    705. /**
    706. * <p>
    707. * 通过key给field设置指定的值,如果key不存在则先创建,如果field已经存在,返回0
    708. * </p>
    709. *
    710. * @param key
    711. * @param field
    712. * @param value
    713. * @return
    714. */
    715. public Long hsetnx(String key, String field, String value) {
    716. Jedis jedis = null;
    717. Long res = null;
    718. try {
    719. jedis = jedisPool.getResource();
    720. res = jedis.hsetnx(key, field, value);
    721. } catch (Exception e) {
    722. log.error(e.getMessage());
    723. } finally {
    724. returnResource(jedisPool, jedis);
    725. }
    726. return res;
    727. }
    728. /**
    729. * <p>
    730. * 通过key同时设置 hash的多个field
    731. * </p>
    732. *
    733. * @param key
    734. * @param hash
    735. * @return 返回OK 异常返回null
    736. */
    737. public String hmset(String key, Map<String, String> hash, int indexdb) {
    738. Jedis jedis = null;
    739. String res = null;
    740. try {
    741. jedis = jedisPool.getResource();
    742. jedis.select(indexdb);
    743. res = jedis.hmset(key, hash);
    744. } catch (Exception e) {
    745. log.error(e.getMessage());
    746. } finally {
    747. returnResource(jedisPool, jedis);
    748. }
    749. return res;
    750. }
    751. /**
    752. * <p>
    753. * 通过key 和 field 获取指定的 value
    754. * </p>
    755. *
    756. * @param key
    757. * @param field
    758. * @return 没有返回null
    759. */
    760. public String hget(String key, String field) {
    761. Jedis jedis = null;
    762. String res = null;
    763. try {
    764. jedis = jedisPool.getResource();
    765. res = jedis.hget(key, field);
    766. } catch (Exception e) {
    767. log.error(e.getMessage());
    768. } finally {
    769. returnResource(jedisPool, jedis);
    770. }
    771. return res;
    772. }
    773. /**
    774. * <p>
    775. * 通过key 和 fields 获取指定的value 如果没有对应的value则返回null
    776. * </p>
    777. *
    778. * @param key
    779. * @param fields
    780. * 可以使 一个String 也可以是 String数组
    781. * @return
    782. */
    783. public List<String> hmget(String key, int indexdb, String... fields) {
    784. Jedis jedis = null;
    785. List<String> res = null;
    786. try {
    787. jedis = jedisPool.getResource();
    788. jedis.select(indexdb);
    789. res = jedis.hmget(key, fields);
    790. } catch (Exception e) {
    791. log.error(e.getMessage());
    792. } finally {
    793. returnResource(jedisPool, jedis);
    794. }
    795. return res;
    796. }
    797. /**
    798. * <p>
    799. * 通过key给指定的field的value加上给定的值
    800. * </p>
    801. *
    802. * @param key
    803. * @param field
    804. * @param value
    805. * @return
    806. */
    807. public Long hincrby(String key, String field, Long value) {
    808. Jedis jedis = null;
    809. Long res = null;
    810. try {
    811. jedis = jedisPool.getResource();
    812. res = jedis.hincrBy(key, field, value);
    813. } catch (Exception e) {
    814. log.error(e.getMessage());
    815. } finally {
    816. returnResource(jedisPool, jedis);
    817. }
    818. return res;
    819. }
    820. /**
    821. * <p>
    822. * 通过key和field判断是否有指定的value存在
    823. * </p>
    824. *
    825. * @param key
    826. * @param field
    827. * @return
    828. */
    829. public Boolean hexists(String key, String field) {
    830. Jedis jedis = null;
    831. Boolean res = false;
    832. try {
    833. jedis = jedisPool.getResource();
    834. res = jedis.hexists(key, field);
    835. } catch (Exception e) {
    836. log.error(e.getMessage());
    837. } finally {
    838. returnResource(jedisPool, jedis);
    839. }
    840. return res;
    841. }
    842. /**
    843. * <p>
    844. * 通过key返回field的数量
    845. * </p>
    846. *
    847. * @param key
    848. * @return
    849. */
    850. public Long hlen(String key) {
    851. Jedis jedis = null;
    852. Long res = null;
    853. try {
    854. jedis = jedisPool.getResource();
    855. res = jedis.hlen(key);
    856. } catch (Exception e) {
    857. log.error(e.getMessage());
    858. } finally {
    859. returnResource(jedisPool, jedis);
    860. }
    861. return res;
    862. }
    863. /**
    864. * <p>
    865. * 通过key 删除指定的 field
    866. * </p>
    867. *
    868. * @param key
    869. * @param fields
    870. * 可以是 一个 field 也可以是 一个数组
    871. * @return
    872. */
    873. public Long hdel(String key, String... fields) {
    874. Jedis jedis = null;
    875. Long res = null;
    876. try {
    877. jedis = jedisPool.getResource();
    878. res = jedis.hdel(key, fields);
    879. } catch (Exception e) {
    880. log.error(e.getMessage());
    881. } finally {
    882. returnResource(jedisPool, jedis);
    883. }
    884. return res;
    885. }
    886. /**
    887. * <p>
    888. * 通过key返回所有的field
    889. * </p>
    890. *
    891. * @param key
    892. * @return
    893. */
    894. public Set<String> hkeys(String key) {
    895. Jedis jedis = null;
    896. Set<String> res = null;
    897. try {
    898. jedis = jedisPool.getResource();
    899. res = jedis.hkeys(key);
    900. } catch (Exception e) {
    901. log.error(e.getMessage());
    902. } finally {
    903. returnResource(jedisPool, jedis);
    904. }
    905. return res;
    906. }
    907. /**
    908. * <p>
    909. * 通过key返回所有和key有关的value
    910. * </p>
    911. *
    912. * @param key
    913. * @return
    914. */
    915. public List<String> hvals(String key) {
    916. Jedis jedis = null;
    917. List<String> res = null;
    918. try {
    919. jedis = jedisPool.getResource();
    920. res = jedis.hvals(key);
    921. } catch (Exception e) {
    922. log.error(e.getMessage());
    923. } finally {
    924. returnResource(jedisPool, jedis);
    925. }
    926. return res;
    927. }
    928. /**
    929. * <p>
    930. * 通过key获取所有的field和value
    931. * </p>
    932. *
    933. * @param key
    934. * @return
    935. */
    936. public Map<String, String> hgetall(String key, int indexdb) {
    937. Jedis jedis = null;
    938. Map<String, String> res = null;
    939. try {
    940. jedis = jedisPool.getResource();
    941. jedis.select(indexdb);
    942. res = jedis.hgetAll(key);
    943. } catch (Exception e) {
    944. log.error(e.getMessage());
    945. } finally {
    946. returnResource(jedisPool, jedis);
    947. }
    948. return res;
    949. }
    950. /**
    951. * <p>
    952. * 通过key向list头部添加字符串
    953. * </p>
    954. *
    955. * @param key
    956. * @param strs
    957. * 可以使一个string 也可以使string数组
    958. * @return 返回list的value个数
    959. */
    960. public Long lpush(int indexdb, String key, String... strs) {
    961. Jedis jedis = null;
    962. Long res = null;
    963. try {
    964. jedis = jedisPool.getResource();
    965. jedis.select(indexdb);
    966. res = jedis.lpush(key, strs);
    967. } catch (Exception e) {
    968. log.error(e.getMessage());
    969. } finally {
    970. returnResource(jedisPool, jedis);
    971. }
    972. return res;
    973. }
    974. /**
    975. * <p>
    976. * 通过key向list尾部添加字符串
    977. * </p>
    978. *
    979. * @param key
    980. * @param strs
    981. * 可以使一个string 也可以使string数组
    982. * @return 返回list的value个数
    983. */
    984. public Long rpush(String key, String... strs) {
    985. Jedis jedis = null;
    986. Long res = null;
    987. try {
    988. jedis = jedisPool.getResource();
    989. res = jedis.rpush(key, strs);
    990. } catch (Exception e) {
    991. log.error(e.getMessage());
    992. } finally {
    993. returnResource(jedisPool, jedis);
    994. }
    995. return res;
    996. }
    997. /**
    998. * <p>
    999. * 通过key在list指定的位置之前或者之后 添加字符串元素
    1000. * </p>
    1001. *
    1002. * @param key
    1003. * @param where
    1004. * LIST_POSITION枚举类型
    1005. * @param pivot
    1006. * list里面的value
    1007. * @param value
    1008. * 添加的value
    1009. * @return
    1010. */
    1011. public Long linsert(String key, LIST_POSITION where, String pivot,
    1012. String value) {
    1013. Jedis jedis = null;
    1014. Long res = null;
    1015. try {
    1016. jedis = jedisPool.getResource();
    1017. res = jedis.linsert(key, where, pivot, value);
    1018. } catch (Exception e) {
    1019. log.error(e.getMessage());
    1020. } finally {
    1021. returnResource(jedisPool, jedis);
    1022. }
    1023. return res;
    1024. }
    1025. /**
    1026. * <p>
    1027. * 通过key设置list指定下标位置的value
    1028. * </p>
    1029. * <p>
    1030. * 如果下标超过list里面value的个数则报错
    1031. * </p>
    1032. *
    1033. * @param key
    1034. * @param index
    1035. * 从0开始
    1036. * @param value
    1037. * @return 成功返回OK
    1038. */
    1039. public String lset(String key, Long index, String value) {
    1040. Jedis jedis = null;
    1041. String res = null;
    1042. try {
    1043. jedis = jedisPool.getResource();
    1044. res = jedis.lset(key, index, value);
    1045. } catch (Exception e) {
    1046. log.error(e.getMessage());
    1047. } finally {
    1048. returnResource(jedisPool, jedis);
    1049. }
    1050. return res;
    1051. }
    1052. /**
    1053. * <p>
    1054. * 通过key从对应的list中删除指定的count个 和 value相同的元素
    1055. * </p>
    1056. *
    1057. * @param key
    1058. * @param count
    1059. * 当count为0时删除全部
    1060. * @param value
    1061. * @return 返回被删除的个数
    1062. */
    1063. public Long lrem(String key, long count, String value) {
    1064. Jedis jedis = null;
    1065. Long res = null;
    1066. try {
    1067. jedis = jedisPool.getResource();
    1068. res = jedis.lrem(key, count, value);
    1069. } catch (Exception e) {
    1070. log.error(e.getMessage());
    1071. } finally {
    1072. returnResource(jedisPool, jedis);
    1073. }
    1074. return res;
    1075. }
    1076. /**
    1077. * <p>
    1078. * 通过key保留list中从strat下标开始到end下标结束的value值
    1079. * </p>
    1080. *
    1081. * @param key
    1082. * @param start
    1083. * @param end
    1084. * @return 成功返回OK
    1085. */
    1086. public String ltrim(String key, long start, long end) {
    1087. Jedis jedis = null;
    1088. String res = null;
    1089. try {
    1090. jedis = jedisPool.getResource();
    1091. res = jedis.ltrim(key, start, end);
    1092. } catch (Exception e) {
    1093. log.error(e.getMessage());
    1094. } finally {
    1095. returnResource(jedisPool, jedis);
    1096. }
    1097. return res;
    1098. }
    1099. /**
    1100. * <p>
    1101. * 通过key从list的头部删除一个value,并返回该value
    1102. * </p>
    1103. *
    1104. * @param key
    1105. * @return
    1106. */
    1107. synchronized public String lpop(String key) {
    1108. Jedis jedis = null;
    1109. String res = null;
    1110. try {
    1111. jedis = jedisPool.getResource();
    1112. res = jedis.lpop(key);
    1113. } catch (Exception e) {
    1114. log.error(e.getMessage());
    1115. } finally {
    1116. returnResource(jedisPool, jedis);
    1117. }
    1118. return res;
    1119. }
    1120. /**
    1121. * <p>
    1122. * 通过key从list尾部删除一个value,并返回该元素
    1123. * </p>
    1124. *
    1125. * @param key
    1126. * @return
    1127. */
    1128. synchronized public String rpop(String key, int indexdb) {
    1129. Jedis jedis = null;
    1130. String res = null;
    1131. try {
    1132. jedis = jedisPool.getResource();
    1133. jedis.select(indexdb);
    1134. res = jedis.rpop(key);
    1135. } catch (Exception e) {
    1136. log.error(e.getMessage());
    1137. } finally {
    1138. returnResource(jedisPool, jedis);
    1139. }
    1140. return res;
    1141. }
    1142. /**
    1143. * <p>
    1144. * 通过key从一个list的尾部删除一个value并添加到另一个list的头部,并返回该value
    1145. * </p>
    1146. * <p>
    1147. * 如果第一个list为空或者不存在则返回null
    1148. * </p>
    1149. *
    1150. * @param srckey
    1151. * @param dstkey
    1152. * @return
    1153. */
    1154. public String rpoplpush(String srckey, String dstkey, int indexdb) {
    1155. Jedis jedis = null;
    1156. String res = null;
    1157. try {
    1158. jedis = jedisPool.getResource();
    1159. jedis.select(indexdb);
    1160. res = jedis.rpoplpush(srckey, dstkey);
    1161. } catch (Exception e) {
    1162. log.error(e.getMessage());
    1163. } finally {
    1164. returnResource(jedisPool, jedis);
    1165. }
    1166. return res;
    1167. }
    1168. /**
    1169. * <p>
    1170. * 通过key获取list中指定下标位置的value
    1171. * </p>
    1172. *
    1173. * @param key
    1174. * @param index
    1175. * @return 如果没有返回null
    1176. */
    1177. public String lindex(String key, long index) {
    1178. Jedis jedis = null;
    1179. String res = null;
    1180. try {
    1181. jedis = jedisPool.getResource();
    1182. res = jedis.lindex(key, index);
    1183. } catch (Exception e) {
    1184. log.error(e.getMessage());
    1185. } finally {
    1186. returnResource(jedisPool, jedis);
    1187. }
    1188. return res;
    1189. }
    1190. /**
    1191. * <p>
    1192. * 通过key返回list的长度
    1193. * </p>
    1194. *
    1195. * @param key
    1196. * @return
    1197. */
    1198. public Long llen(String key) {
    1199. Jedis jedis = null;
    1200. Long res = null;
    1201. try {
    1202. jedis = jedisPool.getResource();
    1203. res = jedis.llen(key);
    1204. } catch (Exception e) {
    1205. log.error(e.getMessage());
    1206. } finally {
    1207. returnResource(jedisPool, jedis);
    1208. }
    1209. return res;
    1210. }
    1211. /**
    1212. * <p>
    1213. * 通过key获取list指定下标位置的value
    1214. * </p>
    1215. * <p>
    1216. * 如果start 为 0 end 为 -1 则返回全部的list中的value
    1217. * </p>
    1218. *
    1219. * @param key
    1220. * @param start
    1221. * @param end
    1222. * @return
    1223. */
    1224. public List<String> lrange(String key, long start, long end, int indexdb) {
    1225. Jedis jedis = null;
    1226. List<String> res = null;
    1227. try {
    1228. jedis = jedisPool.getResource();
    1229. jedis.select(indexdb);
    1230. res = jedis.lrange(key, start, end);
    1231. } catch (Exception e) {
    1232. log.error(e.getMessage());
    1233. } finally {
    1234. returnResource(jedisPool, jedis);
    1235. }
    1236. return res;
    1237. }
    1238. /**
    1239. * <p>
    1240. * 将列表 key 下标为 index 的元素的值设置为 value
    1241. * </p>
    1242. *
    1243. * @param key
    1244. * @param index
    1245. * @param value
    1246. * @return 操作成功返回 ok ,否则返回错误信息
    1247. */
    1248. public String lset(String key, long index, String value) {
    1249. Jedis jedis = null;
    1250. try {
    1251. jedis = jedisPool.getResource();
    1252. return jedis.lset(key, index, value);
    1253. } catch (Exception e) {
    1254. log.error(e.getMessage());
    1255. } finally {
    1256. returnResource(jedisPool, jedis);
    1257. }
    1258. return null;
    1259. }
    1260. /**
    1261. * <p>
    1262. * 返回给定排序后的结果
    1263. * </p>
    1264. *
    1265. * @param key
    1266. * @param sortingParameters
    1267. * @return 返回列表形式的排序结果
    1268. */
    1269. public List<String> sort(String key, SortingParams sortingParameters) {
    1270. Jedis jedis = null;
    1271. try {
    1272. jedis = jedisPool.getResource();
    1273. return jedis.sort(key, sortingParameters);
    1274. } catch (Exception e) {
    1275. log.error(e.getMessage());
    1276. } finally {
    1277. returnResource(jedisPool, jedis);
    1278. }
    1279. return null;
    1280. }
    1281. /**
    1282. * <p>
    1283. * 返回排序后的结果,排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较。
    1284. * </p>
    1285. *
    1286. * @param key
    1287. * @return 返回列表形式的排序结果
    1288. */
    1289. public List<String> sort(String key) {
    1290. Jedis jedis = null;
    1291. try {
    1292. jedis = jedisPool.getResource();
    1293. return jedis.sort(key);
    1294. } catch (Exception e) {
    1295. log.error(e.getMessage());
    1296. } finally {
    1297. returnResource(jedisPool, jedis);
    1298. }
    1299. return null;
    1300. }
    1301. /**
    1302. * <p>
    1303. * 通过key向指定的set中添加value
    1304. * </p>
    1305. *
    1306. * @param key
    1307. * @param members
    1308. * 可以是一个String 也可以是一个String数组
    1309. * @return 添加成功的个数
    1310. */
    1311. public Long sadd(String key, String... members) {
    1312. Jedis jedis = null;
    1313. Long res = null;
    1314. try {
    1315. jedis = jedisPool.getResource();
    1316. res = jedis.sadd(key, members);
    1317. } catch (Exception e) {
    1318. log.error(e.getMessage());
    1319. } finally {
    1320. returnResource(jedisPool, jedis);
    1321. }
    1322. return res;
    1323. }
    1324. /**
    1325. * <p>
    1326. * 通过key删除set中对应的value值
    1327. * </p>
    1328. *
    1329. * @param key
    1330. * @param members
    1331. * 可以是一个String 也可以是一个String数组
    1332. * @return 删除的个数
    1333. */
    1334. public Long srem(String key, String... members) {
    1335. Jedis jedis = null;
    1336. Long res = null;
    1337. try {
    1338. jedis = jedisPool.getResource();
    1339. res = jedis.srem(key, members);
    1340. } catch (Exception e) {
    1341. log.error(e.getMessage());
    1342. } finally {
    1343. returnResource(jedisPool, jedis);
    1344. }
    1345. return res;
    1346. }
    1347. /**
    1348. * <p>
    1349. * 通过key随机删除一个set中的value并返回该值
    1350. * </p>
    1351. *
    1352. * @param key
    1353. * @return
    1354. */
    1355. public String spop(String key) {
    1356. Jedis jedis = null;
    1357. String res = null;
    1358. try {
    1359. jedis = jedisPool.getResource();
    1360. res = jedis.spop(key);
    1361. } catch (Exception e) {
    1362. log.error(e.getMessage());
    1363. } finally {
    1364. returnResource(jedisPool, jedis);
    1365. }
    1366. return res;
    1367. }
    1368. /**
    1369. * <p>
    1370. * 通过key获取set中的差集
    1371. * </p>
    1372. * <p>
    1373. * 以第一个set为标准
    1374. * </p>
    1375. *
    1376. * @param keys
    1377. * 可以使一个string 则返回set中所有的value 也可以是string数组
    1378. * @return
    1379. */
    1380. public Set<String> sdiff(String... keys) {
    1381. Jedis jedis = null;
    1382. Set<String> res = null;
    1383. try {
    1384. jedis = jedisPool.getResource();
    1385. res = jedis.sdiff(keys);
    1386. } catch (Exception e) {
    1387. log.error(e.getMessage());
    1388. } finally {
    1389. returnResource(jedisPool, jedis);
    1390. }
    1391. return res;
    1392. }
    1393. /**
    1394. * <p>
    1395. * 通过key获取set中的差集并存入到另一个key中
    1396. * </p>
    1397. * <p>
    1398. * 以第一个set为标准
    1399. * </p>
    1400. *
    1401. * @param dstkey
    1402. * 差集存入的key
    1403. * @param keys
    1404. * 可以使一个string 则返回set中所有的value 也可以是string数组
    1405. * @return
    1406. */
    1407. public Long sdiffstore(String dstkey, String... keys) {
    1408. Jedis jedis = null;
    1409. Long res = null;
    1410. try {
    1411. jedis = jedisPool.getResource();
    1412. res = jedis.sdiffstore(dstkey, keys);
    1413. } catch (Exception e) {
    1414. log.error(e.getMessage());
    1415. } finally {
    1416. returnResource(jedisPool, jedis);
    1417. }
    1418. return res;
    1419. }
    1420. /**
    1421. * <p>
    1422. * 通过key获取指定set中的交集
    1423. * </p>
    1424. *
    1425. * @param keys
    1426. * 可以使一个string 也可以是一个string数组
    1427. * @return
    1428. */
    1429. public Set<String> sinter(String... keys) {
    1430. Jedis jedis = null;
    1431. Set<String> res = null;
    1432. try {
    1433. jedis = jedisPool.getResource();
    1434. res = jedis.sinter(keys);
    1435. } catch (Exception e) {
    1436. log.error(e.getMessage());
    1437. } finally {
    1438. returnResource(jedisPool, jedis);
    1439. }
    1440. return res;
    1441. }
    1442. /**
    1443. * <p>
    1444. * 通过key获取指定set中的交集 并将结果存入新的set中
    1445. * </p>
    1446. *
    1447. * @param dstkey
    1448. * @param keys
    1449. * 可以使一个string 也可以是一个string数组
    1450. * @return
    1451. */
    1452. public Long sinterstore(String dstkey, String... keys) {
    1453. Jedis jedis = null;
    1454. Long res = null;
    1455. try {
    1456. jedis = jedisPool.getResource();
    1457. res = jedis.sinterstore(dstkey, keys);
    1458. } catch (Exception e) {
    1459. log.error(e.getMessage());
    1460. } finally {
    1461. returnResource(jedisPool, jedis);
    1462. }
    1463. return res;
    1464. }
    1465. /**
    1466. * <p>
    1467. * 通过key返回所有set的并集
    1468. * </p>
    1469. *
    1470. * @param keys
    1471. * 可以使一个string 也可以是一个string数组
    1472. * @return
    1473. */
    1474. public Set<String> sunion(String... keys) {
    1475. Jedis jedis = null;
    1476. Set<String> res = null;
    1477. try {
    1478. jedis = jedisPool.getResource();
    1479. res = jedis.sunion(keys);
    1480. } catch (Exception e) {
    1481. log.error(e.getMessage());
    1482. } finally {
    1483. returnResource(jedisPool, jedis);
    1484. }
    1485. return res;
    1486. }
    1487. /**
    1488. * <p>
    1489. * 通过key返回所有set的并集,并存入到新的set中
    1490. * </p>
    1491. *
    1492. * @param dstkey
    1493. * @param keys
    1494. * 可以使一个string 也可以是一个string数组
    1495. * @return
    1496. */
    1497. public Long sunionstore(String dstkey, String... keys) {
    1498. Jedis jedis = null;
    1499. Long res = null;
    1500. try {
    1501. jedis = jedisPool.getResource();
    1502. res = jedis.sunionstore(dstkey, keys);
    1503. } catch (Exception e) {
    1504. log.error(e.getMessage());
    1505. } finally {
    1506. returnResource(jedisPool, jedis);
    1507. }
    1508. return res;
    1509. }
    1510. /**
    1511. * <p>
    1512. * 通过key将set中的value移除并添加到第二个set中
    1513. * </p>
    1514. *
    1515. * @param srckey
    1516. * 需要移除的
    1517. * @param dstkey
    1518. * 添加的
    1519. * @param member
    1520. * set中的value
    1521. * @return
    1522. */
    1523. public Long smove(String srckey, String dstkey, String member) {
    1524. Jedis jedis = null;
    1525. Long res = null;
    1526. try {
    1527. jedis = jedisPool.getResource();
    1528. res = jedis.smove(srckey, dstkey, member);
    1529. } catch (Exception e) {
    1530. log.error(e.getMessage());
    1531. } finally {
    1532. returnResource(jedisPool, jedis);
    1533. }
    1534. return res;
    1535. }
    1536. /**
    1537. * <p>
    1538. * 通过key获取set中value的个数
    1539. * </p>
    1540. *
    1541. * @param key
    1542. * @return
    1543. */
    1544. public Long scard(String key) {
    1545. Jedis jedis = null;
    1546. Long res = null;
    1547. try {
    1548. jedis = jedisPool.getResource();
    1549. res = jedis.scard(key);
    1550. } catch (Exception e) {
    1551. log.error(e.getMessage());
    1552. } finally {
    1553. returnResource(jedisPool, jedis);
    1554. }
    1555. return res;
    1556. }
    1557. /**
    1558. * <p>
    1559. * 通过key判断value是否是set中的元素
    1560. * </p>
    1561. *
    1562. * @param key
    1563. * @param member
    1564. * @return
    1565. */
    1566. public Boolean sismember(String key, String member) {
    1567. Jedis jedis = null;
    1568. Boolean res = null;
    1569. try {
    1570. jedis = jedisPool.getResource();
    1571. res = jedis.sismember(key, member);
    1572. } catch (Exception e) {
    1573. log.error(e.getMessage());
    1574. } finally {
    1575. returnResource(jedisPool, jedis);
    1576. }
    1577. return res;
    1578. }
    1579. /**
    1580. * <p>
    1581. * 通过key获取set中随机的value,不删除元素
    1582. * </p>
    1583. *
    1584. * @param key
    1585. * @return
    1586. */
    1587. public String srandmember(String key) {
    1588. Jedis jedis = null;
    1589. String res = null;
    1590. try {
    1591. jedis = jedisPool.getResource();
    1592. res = jedis.srandmember(key);
    1593. } catch (Exception e) {
    1594. log.error(e.getMessage());
    1595. } finally {
    1596. returnResource(jedisPool, jedis);
    1597. }
    1598. return res;
    1599. }
    1600. /**
    1601. * <p>
    1602. * 通过key获取set中所有的value
    1603. * </p>
    1604. *
    1605. * @param key
    1606. * @return
    1607. */
    1608. public Set<String> smembers(String key) {
    1609. Jedis jedis = null;
    1610. Set<String> res = null;
    1611. try {
    1612. jedis = jedisPool.getResource();
    1613. res = jedis.smembers(key);
    1614. } catch (Exception e) {
    1615. log.error(e.getMessage());
    1616. } finally {
    1617. returnResource(jedisPool, jedis);
    1618. }
    1619. return res;
    1620. }
    1621. /**
    1622. * <p>
    1623. * 通过key向zset中添加value,score,其中score就是用来排序的
    1624. * </p>
    1625. * <p>
    1626. * 如果该value已经存在则根据score更新元素
    1627. * </p>
    1628. *
    1629. * @param key
    1630. * @param score
    1631. * @param member
    1632. * @return
    1633. */
    1634. public Long zadd(String key, double score, String member) {
    1635. Jedis jedis = null;
    1636. Long res = null;
    1637. try {
    1638. jedis = jedisPool.getResource();
    1639. res = jedis.zadd(key, score, member);
    1640. } catch (Exception e) {
    1641. log.error(e.getMessage());
    1642. } finally {
    1643. returnResource(jedisPool, jedis);
    1644. }
    1645. return res;
    1646. }
    1647. /**
    1648. * <p>
    1649. * 返回有序集 key 中,指定区间内的成员。min=0,max=-1代表所有元素
    1650. * </p>
    1651. *
    1652. * @param key
    1653. * @param min
    1654. * @param max
    1655. * @return 指定区间内的有序集成员的列表。
    1656. */
    1657. public Set<String> zrange(String key, long min, long max) {
    1658. Jedis jedis = null;
    1659. try {
    1660. jedis = jedisPool.getResource();
    1661. return jedis.zrange(key, min, max);
    1662. } catch (Exception e) {
    1663. log.error(e.getMessage());
    1664. } finally {
    1665. returnResource(jedisPool, jedis);
    1666. }
    1667. return null;
    1668. }
    1669. /**
    1670. * <p>
    1671. * 统计有序集 key 中,值在 min 和 max 之间的成员的数量
    1672. * </p>
    1673. *
    1674. * @param key
    1675. * @param min
    1676. * @param max
    1677. * @return 值在 min 和 max 之间的成员的数量。异常返回0
    1678. */
    1679. public Long zcount(String key, double min, double max) {
    1680. Jedis jedis = null;
    1681. try {
    1682. jedis = jedisPool.getResource();
    1683. return jedis.zcount(key, min, max);
    1684. } catch (Exception e) {
    1685. log.error(e.getMessage());
    1686. return 0L;
    1687. } finally {
    1688. returnResource(jedisPool, jedis);
    1689. }
    1690. }
    1691. /**
    1692. * <p>
    1693. * 为哈希表 key 中的域 field 的值加上增量 increment 。增量也可以为负数,相当于对给定域进行减法操作。 如果 key
    1694. * 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。
    1695. * 对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。本操作的值被限制在 64 位(bit)有符号数字表示之内。
    1696. * </p>
    1697. * <p>
    1698. * 将名称为key的hash中field的value增加integer
    1699. * </p>
    1700. *
    1701. * @param key
    1702. * @param value
    1703. * @param increment
    1704. * @return 执行 HINCRBY 命令之后,哈希表 key 中域 field的值。异常返回0
    1705. */
    1706. public Long hincrBy(String key, String value, long increment) {
    1707. Jedis jedis = null;
    1708. try {
    1709. jedis = jedisPool.getResource();
    1710. return jedis.hincrBy(key, value, increment);
    1711. } catch (Exception e) {
    1712. log.error(e.getMessage());
    1713. return 0L;
    1714. } finally {
    1715. returnResource(jedisPool, jedis);
    1716. }
    1717. }
    1718. /**
    1719. * <p>
    1720. * 通过key删除在zset中指定的value
    1721. * </p>
    1722. *
    1723. * @param key
    1724. * @param members
    1725. * 可以使一个string 也可以是一个string数组
    1726. * @return
    1727. */
    1728. public Long zrem(String key, String... members) {
    1729. Jedis jedis = null;
    1730. Long res = null;
    1731. try {
    1732. jedis = jedisPool.getResource();
    1733. res = jedis.zrem(key, members);
    1734. } catch (Exception e) {
    1735. log.error(e.getMessage());
    1736. } finally {
    1737. returnResource(jedisPool, jedis);
    1738. }
    1739. return res;
    1740. }
    1741. /**
    1742. * <p>
    1743. * 通过key增加该zset中value的score的值
    1744. * </p>
    1745. *
    1746. * @param key
    1747. * @param score
    1748. * @param member
    1749. * @return
    1750. */
    1751. public Double zincrby(String key, double score, String member) {
    1752. Jedis jedis = null;
    1753. Double res = null;
    1754. try {
    1755. jedis = jedisPool.getResource();
    1756. res = jedis.zincrby(key, score, member);
    1757. } catch (Exception e) {
    1758. log.error(e.getMessage());
    1759. } finally {
    1760. returnResource(jedisPool, jedis);
    1761. }
    1762. return res;
    1763. }
    1764. /**
    1765. * <p>
    1766. * 通过key返回zset中value的排名
    1767. * </p>
    1768. * <p>
    1769. * 下标从小到大排序
    1770. * </p>
    1771. *
    1772. * @param key
    1773. * @param member
    1774. * @return
    1775. */
    1776. public Long zrank(String key, String member) {
    1777. Jedis jedis = null;
    1778. Long res = null;
    1779. try {
    1780. jedis = jedisPool.getResource();
    1781. res = jedis.zrank(key, member);
    1782. } catch (Exception e) {
    1783. log.error(e.getMessage());
    1784. } finally {
    1785. returnResource(jedisPool, jedis);
    1786. }
    1787. return res;
    1788. }
    1789. /**
    1790. * <p>
    1791. * 通过key返回zset中value的排名
    1792. * </p>
    1793. * <p>
    1794. * 下标从大到小排序
    1795. * </p>
    1796. *
    1797. * @param key
    1798. * @param member
    1799. * @return
    1800. */
    1801. public Long zrevrank(String key, String member) {
    1802. Jedis jedis = null;
    1803. Long res = null;
    1804. try {
    1805. jedis = jedisPool.getResource();
    1806. res = jedis.zrevrank(key, member);
    1807. } catch (Exception e) {
    1808. log.error(e.getMessage());
    1809. } finally {
    1810. returnResource(jedisPool, jedis);
    1811. }
    1812. return res;
    1813. }
    1814. /**
    1815. * <p>
    1816. * 通过key将获取score从start到end中zset的value
    1817. * </p>
    1818. * <p>
    1819. * socre从大到小排序
    1820. * </p>
    1821. * <p>
    1822. * 当start为0 end为-1时返回全部
    1823. * </p>
    1824. *
    1825. * @param key
    1826. * @param start
    1827. * @param end
    1828. * @return
    1829. */
    1830. public Set<String> zrevrange(String key, long start, long end) {
    1831. Jedis jedis = null;
    1832. Set<String> res = null;
    1833. try {
    1834. jedis = jedisPool.getResource();
    1835. res = jedis.zrevrange(key, start, end);
    1836. } catch (Exception e) {
    1837. log.error(e.getMessage());
    1838. } finally {
    1839. returnResource(jedisPool, jedis);
    1840. }
    1841. return res;
    1842. }
    1843. /**
    1844. * <p>
    1845. * 通过key返回指定score内zset中的value
    1846. * </p>
    1847. *
    1848. * @param key
    1849. * @param max
    1850. * @param min
    1851. * @return
    1852. */
    1853. public Set<String> zrangebyscore(String key, String max, String min) {
    1854. Jedis jedis = null;
    1855. Set<String> res = null;
    1856. try {
    1857. jedis = jedisPool.getResource();
    1858. res = jedis.zrevrangeByScore(key, max, min);
    1859. } catch (Exception e) {
    1860. log.error(e.getMessage());
    1861. } finally {
    1862. returnResource(jedisPool, jedis);
    1863. }
    1864. return res;
    1865. }
    1866. /**
    1867. * <p>
    1868. * 通过key返回指定score内zset中的value
    1869. * </p>
    1870. *
    1871. * @param key
    1872. * @param max
    1873. * @param min
    1874. * @return
    1875. */
    1876. public Set<String> zrangeByScore(String key, double max, double min) {
    1877. Jedis jedis = null;
    1878. Set<String> res = null;
    1879. try {
    1880. jedis = jedisPool.getResource();
    1881. res = jedis.zrevrangeByScore(key, max, min);
    1882. } catch (Exception e) {
    1883. log.error(e.getMessage());
    1884. } finally {
    1885. returnResource(jedisPool, jedis);
    1886. }
    1887. return res;
    1888. }
    1889. /**
    1890. * <p>
    1891. * 返回指定区间内zset中value的数量
    1892. * </p>
    1893. *
    1894. * @param key
    1895. * @param min
    1896. * @param max
    1897. * @return
    1898. */
    1899. public Long zcount(String key, String min, String max) {
    1900. Jedis jedis = null;
    1901. Long res = null;
    1902. try {
    1903. jedis = jedisPool.getResource();
    1904. res = jedis.zcount(key, min, max);
    1905. } catch (Exception e) {
    1906. log.error(e.getMessage());
    1907. } finally {
    1908. returnResource(jedisPool, jedis);
    1909. }
    1910. return res;
    1911. }
    1912. /**
    1913. * <p>
    1914. * 通过key返回zset中的value个数
    1915. * </p>
    1916. *
    1917. * @param key
    1918. * @return
    1919. */
    1920. public Long zcard(String key) {
    1921. Jedis jedis = null;
    1922. Long res = null;
    1923. try {
    1924. jedis = jedisPool.getResource();
    1925. res = jedis.zcard(key);
    1926. } catch (Exception e) {
    1927. log.error(e.getMessage());
    1928. } finally {
    1929. returnResource(jedisPool, jedis);
    1930. }
    1931. return res;
    1932. }
    1933. /**
    1934. * <p>
    1935. * 通过key获取zset中value的score值
    1936. * </p>
    1937. *
    1938. * @param key
    1939. * @param member
    1940. * @return
    1941. */
    1942. public Double zscore(String key, String member) {
    1943. Jedis jedis = null;
    1944. Double res = null;
    1945. try {
    1946. jedis = jedisPool.getResource();
    1947. res = jedis.zscore(key, member);
    1948. } catch (Exception e) {
    1949. log.error(e.getMessage());
    1950. } finally {
    1951. returnResource(jedisPool, jedis);
    1952. }
    1953. return res;
    1954. }
    1955. /**
    1956. * <p>
    1957. * 通过key删除给定区间内的元素
    1958. * </p>
    1959. *
    1960. * @param key
    1961. * @param start
    1962. * @param end
    1963. * @return
    1964. */
    1965. public Long zremrangeByRank(String key, long start, long end) {
    1966. Jedis jedis = null;
    1967. Long res = null;
    1968. try {
    1969. jedis = jedisPool.getResource();
    1970. res = jedis.zremrangeByRank(key, start, end);
    1971. } catch (Exception e) {
    1972. log.error(e.getMessage());
    1973. } finally {
    1974. returnResource(jedisPool, jedis);
    1975. }
    1976. return res;
    1977. }
    1978. /**
    1979. * <p>
    1980. * 通过key删除指定score内的元素
    1981. * </p>
    1982. *
    1983. * @param key
    1984. * @param start
    1985. * @param end
    1986. * @return
    1987. */
    1988. public Long zremrangeByScore(String key, double start, double end) {
    1989. Jedis jedis = null;
    1990. Long res = null;
    1991. try {
    1992. jedis = jedisPool.getResource();
    1993. res = jedis.zremrangeByScore(key, start, end);
    1994. } catch (Exception e) {
    1995. log.error(e.getMessage());
    1996. } finally {
    1997. returnResource(jedisPool, jedis);
    1998. }
    1999. return res;
    2000. }
    2001. /**
    2002. * <p>
    2003. * 返回满足pattern表达式的所有key
    2004. * </p>
    2005. * <p>
    2006. * keys(*)
    2007. * </p>
    2008. * <p>
    2009. * 返回所有的key
    2010. * </p>
    2011. *
    2012. * @param pattern
    2013. * @return
    2014. */
    2015. public Set<String> keys(String pattern) {
    2016. Jedis jedis = null;
    2017. Set<String> res = null;
    2018. try {
    2019. jedis = jedisPool.getResource();
    2020. res = jedis.keys(pattern);
    2021. } catch (Exception e) {
    2022. log.error(e.getMessage());
    2023. } finally {
    2024. returnResource(jedisPool, jedis);
    2025. }
    2026. return res;
    2027. }
    2028. public Set<String> keysBySelect(String pattern,int database) {
    2029. Jedis jedis = null;
    2030. Set<String> res = null;
    2031. try {
    2032. jedis = jedisPool.getResource();
    2033. jedis.select(database);
    2034. res = jedis.keys(pattern);
    2035. } catch (Exception e) {
    2036. log.error(e.getMessage());
    2037. } finally {
    2038. returnResource(jedisPool, jedis);
    2039. }
    2040. return res;
    2041. }
    2042. /**
    2043. * <p>
    2044. * 通过key判断值得类型
    2045. * </p>
    2046. *
    2047. * @param key
    2048. * @return
    2049. */
    2050. public String type(String key) {
    2051. Jedis jedis = null;
    2052. String res = null;
    2053. try {
    2054. jedis = jedisPool.getResource();
    2055. res = jedis.type(key);
    2056. } catch (Exception e) {
    2057. log.error(e.getMessage());
    2058. } finally {
    2059. returnResource(jedisPool, jedis);
    2060. }
    2061. return res;
    2062. }
    2063. /**
    2064. * 序列化对象
    2065. * @param obj
    2066. * @return
    2067. * 对象需实现Serializable接口
    2068. */
    2069. public static byte[] ObjTOSerialize(Object obj) {
    2070. ObjectOutputStream oos = null;
    2071. ByteArrayOutputStream byteOut = null;
    2072. try {
    2073. byteOut = new ByteArrayOutputStream();
    2074. oos = new ObjectOutputStream(byteOut);
    2075. oos.writeObject(obj);
    2076. byte[] bytes = byteOut.toByteArray();
    2077. return bytes;
    2078. } catch (Exception e) {
    2079. }
    2080. return null;
    2081. }
    2082. /**
    2083. * 反序列化对象
    2084. * @param bytes
    2085. * @return
    2086. * 对象需实现Serializable接口
    2087. */
    2088. public static Object unserialize(byte[] bytes) {
    2089. ByteArrayInputStream bais = null;
    2090. try {
    2091. //反序列化
    2092. bais = new ByteArrayInputStream(bytes);
    2093. ObjectInputStream ois = new ObjectInputStream(bais);
    2094. return ois.readObject();
    2095. } catch (Exception e) {
    2096. }
    2097. return null;
    2098. }
    2099. /**
    2100. * 返还到连接池
    2101. *
    2102. * @param jedisPool
    2103. * @param jedis
    2104. */
    2105. public static void returnResource(JedisPool jedisPool, Jedis jedis) {
    2106. if (jedis != null) {
    2107. jedisPool.returnResource(jedis);
    2108. }
    2109. }
    2110. // public static RedisUtil getRu() {
    2111. // return ru;
    2112. // }
    2113. //
    2114. // public static void setRu(RedisUtil ru) {
    2115. // RedisUtil.ru = ru;
    2116. // }
    2117. public static void main(String[] args) {
    2118. /*JedisPool jedisPool = new JedisPool(null,"localhost",6379,100,"123456");
    2119. Jedis jedis = jedisPool.getResource();
    2120. //r.get("", RedisConstants.datebase4);
    2121. jedis.select(RedisConstants.datebase4);
    2122. Set<String> str = jedis.keys("*");
    2123. for (String string : str) {
    2124. System.out.println(string);
    2125. }*/
    2126. }
    2127. }

    4.示例说明,同样的,我拿出最基本的set方法,indexdb参数为需要自定义的redis库编码,finally中我们回收此连接放入连接池。

    1. public String set(String key, String value,int indexdb) {
    2. Jedis jedis = null;
    3. try {
    4. jedis = jedisPool.getResource();
    5. jedis.select(indexdb);
    6. return jedis.set(key, value);
    7. } catch (Exception e) {
    8. log.error(e.getMessage());
    9. return "0";
    10. } finally {
    11. returnResource(jedisPool, jedis);
    12. }
    13. }

    二、实际操作示例

    还是一样,这里新建Controller进行测试操作

    RedisController.java

    1. package com.springboot.demo.controller;
    2. import com.springboot.demo.base.controller.BaseController;
    3. import com.springboot.demo.base.utils.RedisConstants;
    4. import com.springboot.demo.base.utils.RedisUtil;
    5. import com.springboot.demo.base.utils.StateParameter;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Controller;
    8. import org.springframework.ui.ModelMap;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RequestMethod;
    11. import org.springframework.web.bind.annotation.ResponseBody;
    12. /**
    13. * @ClassName: RedisController
    14. * @Auther: zhangyingqi
    15. * @Date: 2018/8/29 16:15
    16. * @Description:
    17. */
    18. @Controller
    19. @RequestMapping(value="/redis")
    20. public class RedisController extends BaseController {
    21. @Autowired
    22. RedisUtil redisUtil;
    23. /**
    24. * @auther: zhangyingqi
    25. * @date: 16:23 2018/8/29
    26. * @param: []
    27. * @return: org.springframework.ui.ModelMap
    28. * @Description: 执行redis写/读/生命周期
    29. */
    30. @RequestMapping(value = "getRedis",method = RequestMethod.POST)
    31. @ResponseBody
    32. public ModelMap getRedis(){
    33. redisUtil.set("20182018","这是一条测试数据", RedisConstants.datebase1);
    34. Long resExpire = redisUtil.expire("20182018", 60, RedisConstants.datebase1);//设置key过期时间
    35. logger.info("resExpire="+resExpire);
    36. String res = redisUtil.get("20182018", RedisConstants.datebase1);
    37. return getModelMap(StateParameter.SUCCESS, res, "执行成功");
    38. }
    39. }

    使用@Autowired注入RedisUtil

    使用以下语句存储缓存值

    redisUtil.set("20182018","这是一条测试数据", RedisConstants.datebase1);

    使用以下语句设置该值的生命周期即有效期,单位是秒

    Long resExpire = redisUtil.expire("20182018", 60, RedisConstants.datebase1);//设置key过期时间

    启动项目请求地址:http://localhost:8080/redis/getRedis,返回成功,打开redis客户端工具,可以看到在db1中存入了该数据,并且设置了有效时间为60秒,截图时还剩余54秒。

     最后给出pom包

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>com.springboot</groupId>
    6. <artifactId>springbootRedis2</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <packaging>war</packaging>
    9. <name>springbootRedis2</name>
    10. <description>Demo project for Spring Boot</description>
    11. <parent>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-parent</artifactId>
    14. <version>2.0.4.RELEASE</version>
    15. <relativePath/> <!-- lookup parent from repository -->
    16. </parent>
    17. <properties>
    18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    20. <java.version>1.8</java.version>
    21. </properties>
    22. <dependencies>
    23. <dependency>
    24. <groupId>org.springframework.boot</groupId>
    25. <artifactId>spring-boot-starter-tomcat</artifactId>
    26. <!--<scope>provided</scope>-->
    27. </dependency>
    28. <dependency>
    29. <groupId>org.springframework.boot</groupId>
    30. <artifactId>spring-boot-starter-data-jpa</artifactId>
    31. <exclusions>
    32. <exclusion>
    33. <groupId>org.hibernate</groupId>
    34. <artifactId>hibernate-entitymanager</artifactId>
    35. </exclusion>
    36. <exclusion>
    37. <groupId>org.hibernate</groupId>
    38. <artifactId>hibernate-core</artifactId>
    39. </exclusion>
    40. </exclusions>
    41. </dependency>
    42. <dependency>
    43. <groupId>org.hibernate</groupId>
    44. <artifactId>hibernate-core</artifactId>
    45. <version>5.2.10.Final</version>
    46. </dependency>
    47. <dependency>
    48. <groupId>org.springframework.boot</groupId>
    49. <artifactId>spring-boot-starter-data-redis</artifactId>
    50. </dependency>
    51. <dependency>
    52. <groupId>org.springframework.boot</groupId>
    53. <artifactId>spring-boot-starter-mail</artifactId>
    54. </dependency>
    55. <dependency>
    56. <groupId>org.springframework.boot</groupId>
    57. <artifactId>spring-boot-starter-thymeleaf</artifactId>
    58. </dependency>
    59. <dependency>
    60. <groupId>org.springframework.boot</groupId>
    61. <artifactId>spring-boot-starter-web</artifactId>
    62. </dependency>
    63. <dependency>
    64. <groupId>mysql</groupId>
    65. <artifactId>mysql-connector-java</artifactId>
    66. <scope>runtime</scope>
    67. </dependency>
    68. <dependency>
    69. <groupId>org.projectlombok</groupId>
    70. <artifactId>lombok</artifactId>
    71. <optional>true</optional>
    72. </dependency>
    73. <dependency>
    74. <groupId>org.springframework.boot</groupId>
    75. <artifactId>spring-boot-starter-test</artifactId>
    76. <scope>test</scope>
    77. </dependency>
    78. <dependency>
    79. <groupId>org.springframework.boot</groupId>
    80. <!--<artifactId>spring-boot-starter-redis</artifactId>-->
    81. <artifactId>spring-boot-starter-data-redis</artifactId>
    82. </dependency>
    83. <dependency>
    84. <groupId>redis.clients</groupId>
    85. <artifactId>jedis</artifactId>
    86. <version>2.9.0</version>
    87. </dependency>
    88. </dependencies>
    89. <build>
    90. <plugins>
    91. <plugin>
    92. <groupId>org.springframework.boot</groupId>
    93. <artifactId>spring-boot-maven-plugin</artifactId>
    94. </plugin>
    95. </plugins>
    96. </build>
    97. </project>

    实际上只需引入以下两个依赖即可,注意由于springboot的版本不同,引入redis的配置写法略有不同。我注释掉的是旧版本的springboot引入redis方式,请留意。

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <!--<artifactId>spring-boot-starter-redis</artifactId>-->
    4. <artifactId>spring-boot-starter-data-redis</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>redis.clients</groupId>
    8. <artifactId>jedis</artifactId>
    9. <version>2.9.0</version>
    10. </dependency>

    花了一些时间对这两种在springboot中引入Redis的方式进行了一个小结,实际使用中发现两者并无太大差异,所以任选一个即可。

    写博文不易,转载请注明出处。

    全文完,2018/8/29

  • 相关阅读:
    FMDB增删查改
    https相关内容
    支付宝、微信支付参考博客
    下标脚本(Swift)
    函数(swift)
    控制流(swift)
    UIView Methods
    oc js 交互
    我和Lua并非一见钟情,我们期待着日久生情(相遇篇)
    与Python Falling In Love_Python跨台阶(面向对象)
  • 原文地址:https://www.cnblogs.com/tanghaoran-blog/p/10429607.html
Copyright © 2020-2023  润新知