SpringBoot+单机redis

- 日理万妓 2022-03-30 07:26 287阅读 0赞
  • spring boot-redis集成

    • 看教程来的,看起来很简单,但是集成后发现启动失败?

    WARN 2556 —- [ restartedMain] ationConfigEmbeddedWebApplicationContext :

    Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException:

    Error creating bean with name ‘redisTemplate’ defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]:

      Unsatisfied dependency expressed through method ‘redisTemplate’ parameter 0;

      nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:

      No qualifying bean of type ‘org.springframework.data.redis.connection.RedisConnectionFactory’ available:

       expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

  • 几经磨难最后的结果贴一下:

  • yml配置

  1. spring:
  2. redis:
  3. host: 127.0.0.1
  4. port: 6379
  5. timeout: 10000
  6. pool:
  7. max-active: 8
  8. max-wait: -1
  9. max-idle: 8
  10. min-idle: 0
  11. password: root
  • pom

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-data-redis</artifactId>
    4. <exclusions>
    5. <exclusion>
    6. <groupId>io.lettuce</groupId>
    7. <artifactId>lettuce-core</artifactId>
    8. </exclusion>
    9. </exclusions>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.data</groupId>
    13. <artifactId>spring-data-commons</artifactId>
    14. </dependency>
    15. <dependency>
    16. <groupId>redis.clients</groupId>
    17. <artifactId>jedis</artifactId>
    18. </dependency>

      

  • redisConfig

    1. /**
    2. * redis config
    3. */
    4. @Configuration
    5. @EnableCaching//启用缓存,这个注解很重要
    6. //继承CachingConfigurerSupport,为了自定义生成KEY的策略。可以不继承。
    7. public class RedisConfig {
    8. //缓存管理器
    9. @Bean
    10. public CacheManager cacheManager(RedisTemplate redisTemplate) {
    11. RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    12. return cacheManager;
    13. }
    14. /**
    15. * redisTemplate
    16. * @param factory
    17. * @return
    18. */
    19. @Bean
    20. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    21. StringRedisTemplate template = new StringRedisTemplate(factory);
    22. setSerializer(template);//设置序列化工具
    23. template.afterPropertiesSet();
    24. return template;
    25. }
    26. /**
    27. * 序列化
    28. * @param template
    29. */
    30. private void setSerializer(StringRedisTemplate template) {
    31. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    32. ObjectMapper om = new ObjectMapper();
    33. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    34. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    35. jackson2JsonRedisSerializer.setObjectMapper(om);
    36. template.setValueSerializer(jackson2JsonRedisSerializer);
    37. }
    38. /**
    39. * 生成key的策略
    40. * @return
    41. */
    42. @Bean
    43. public KeyGenerator keyGenerator() {
    44. return new KeyGenerator() {
    45. @Override
    46. public Object generate(Object target, Method method, Object... params) {
    47. StringBuilder sb = new StringBuilder();
    48. sb.append(target.getClass().getName());
    49. sb.append(method.getName());
    50. for (Object obj : params) {
    51. sb.append(obj.toString());
    52. }
    53. return sb.toString();
    54. }
    55. };
    56. }
    57. }
  • redisUtil

    1. /**
    2. * @version V1.0
    3. * @Description: TODO
    4. * @date Date : 2018/12/28 0028 22:46
    5. */
    6. @Component
    7. public class RedisService {
    8. @Autowired
    9. private RedisTemplate redisTemplate;
    10. /**
    11. * 指定缓存失效时间
    12. *
    13. * @param key 键
    14. * @param time 时间(秒)
    15. * @return
    16. */
    17. public boolean expire(String key, long time) {
    18. try {
    19. if (time > 0) {
    20. redisTemplate.expire(key, time, TimeUnit.SECONDS);
    21. }
    22. return true;
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. return false;
    26. }
    27. }
    28. /**
    29. * 根据key 获取过期时间
    30. *
    31. * @param key 键 不能为null
    32. * @return 时间(秒) 返回0代表为永久有效
    33. */
    34. public long getExpire(String key) {
    35. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    36. }
    37. /**
    38. * 判断key是否存在
    39. *
    40. * @param key 键
    41. * @return true 存在 false不存在
    42. */
    43. public boolean hasKey(String key) {
    44. try {
    45. return redisTemplate.hasKey(key);
    46. } catch (Exception e) {
    47. e.printStackTrace();
    48. return false;
    49. }
    50. }
    51. /**
    52. * 删除缓存
    53. *
    54. * @param key 可以传一个值 或多个
    55. */
    56. @SuppressWarnings("unchecked")
    57. public void del(String... key) {
    58. if (key != null && key.length > 0) {
    59. if (key.length == 1) {
    60. redisTemplate.delete(key[0]);
    61. } else {
    62. redisTemplate.delete(CollectionUtils.arrayToList(key));
    63. }
    64. }
    65. }
    66. //============================String=============================
    67. /**
    68. * 普通缓存获取
    69. *
    70. * @param key 键
    71. * @return 值
    72. */
    73. public Object get(String key) {
    74. return key == null ? null : redisTemplate.opsForValue().get(key);
    75. }
    76. /**
    77. * 普通缓存放入
    78. *
    79. * @param key 键
    80. * @param value 值
    81. * @return true成功 false失败
    82. */
    83. public boolean set(String key, Object value) {
    84. try {
    85. redisTemplate.opsForValue().set(key, value);
    86. return true;
    87. } catch (Exception e) {
    88. e.printStackTrace();
    89. return false;
    90. }
    91. }
    92. /**
    93. * 普通缓存放入并设置时间
    94. *
    95. * @param key 键
    96. * @param value 值
    97. * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
    98. * @return true成功 false 失败
    99. */
    100. public boolean set(String key, Object value, long time) {
    101. try {
    102. if (time > 0) {
    103. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    104. } else {
    105. set(key, value);
    106. }
    107. return true;
    108. } catch (Exception e) {
    109. e.printStackTrace();
    110. return false;
    111. }
    112. }
    113. /**
    114. * 递增
    115. *
    116. * @param key 键
    117. * @param delta 要增加几(大于0)
    118. * @return
    119. */
    120. public long incr(String key, long delta) {
    121. if (delta < 0) {
    122. throw new RuntimeException("递增因子必须大于0");
    123. }
    124. return redisTemplate.opsForValue().increment(key, delta);
    125. }
    126. /**
    127. * 递减
    128. *
    129. * @param key 键
    130. * @param delta 要减少几(小于0)
    131. * @return
    132. */
    133. public long decr(String key, long delta) {
    134. if (delta < 0) {
    135. throw new RuntimeException("递减因子必须大于0");
    136. }
    137. return redisTemplate.opsForValue().increment(key, -delta);
    138. }
    139. //================================Map=================================
    140. /**
    141. * HashGet
    142. *
    143. * @param key 键 不能为null
    144. * @param item 项 不能为null
    145. * @return 值
    146. */
    147. public Object hget(String key, String item) {
    148. return redisTemplate.opsForHash().get(key, item);
    149. }
    150. /**
    151. * 获取hashKey对应的所有键值
    152. *
    153. * @param key 键
    154. * @return 对应的多个键值
    155. */
    156. public Map<Object, Object> hmget(String key) {
    157. return redisTemplate.opsForHash().entries(key);
    158. }
    159. /**
    160. * HashSet
    161. *
    162. * @param key 键
    163. * @param map 对应多个键值
    164. * @return true 成功 false 失败
    165. */
    166. public boolean hmset(String key, Map<String, Object> map) {
    167. try {
    168. redisTemplate.opsForHash().putAll(key, map);
    169. return true;
    170. } catch (Exception e) {
    171. e.printStackTrace();
    172. return false;
    173. }
    174. }
    175. /**
    176. * HashSet 并设置时间
    177. *
    178. * @param key 键
    179. * @param map 对应多个键值
    180. * @param time 时间(秒)
    181. * @return true成功 false失败
    182. */
    183. public boolean hmset(String key, Map<String, Object> map, long time) {
    184. try {
    185. redisTemplate.opsForHash().putAll(key, map);
    186. if (time > 0) {
    187. expire(key, time);
    188. }
    189. return true;
    190. } catch (Exception e) {
    191. e.printStackTrace();
    192. return false;
    193. }
    194. }
    195. /**
    196. * 向一张hash表中放入数据,如果不存在将创建
    197. *
    198. * @param key 键
    199. * @param item 项
    200. * @param value 值
    201. * @return true 成功 false失败
    202. */
    203. public boolean hset(String key, String item, Object value) {
    204. try {
    205. redisTemplate.opsForHash().put(key, item, value);
    206. return true;
    207. } catch (Exception e) {
    208. e.printStackTrace();
    209. return false;
    210. }
    211. }
    212. /**
    213. * 向一张hash表中放入数据,如果不存在将创建
    214. *
    215. * @param key 键
    216. * @param item 项
    217. * @param value 值
    218. * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
    219. * @return true 成功 false失败
    220. */
    221. public boolean hset(String key, String item, Object value, long time) {
    222. try {
    223. redisTemplate.opsForHash().put(key, item, value);
    224. if (time > 0) {
    225. expire(key, time);
    226. }
    227. return true;
    228. } catch (Exception e) {
    229. e.printStackTrace();
    230. return false;
    231. }
    232. }
    233. /**
    234. * 删除hash表中的值
    235. *
    236. * @param key 键 不能为null
    237. * @param item 项 可以使多个 不能为null
    238. */
    239. public void hdel(String key, Object... item) {
    240. redisTemplate.opsForHash().delete(key, item);
    241. }
    242. /**
    243. * 判断hash表中是否有该项的值
    244. *
    245. * @param key 键 不能为null
    246. * @param item 项 不能为null
    247. * @return true 存在 false不存在
    248. */
    249. public boolean hHasKey(String key, String item) {
    250. return redisTemplate.opsForHash().hasKey(key, item);
    251. }
    252. /**
    253. * hash递增 如果不存在,就会创建一个 并把新增后的值返回
    254. *
    255. * @param key 键
    256. * @param item 项
    257. * @param by 要增加几(大于0)
    258. * @return
    259. */
    260. public double hincr(String key, String item, double by) {
    261. return redisTemplate.opsForHash().increment(key, item, by);
    262. }
    263. /**
    264. * hash递减
    265. *
    266. * @param key 键
    267. * @param item 项
    268. * @param by 要减少记(小于0)
    269. * @return
    270. */
    271. public double hdecr(String key, String item, double by) {
    272. return redisTemplate.opsForHash().increment(key, item, -by);
    273. }
    274. //============================set=============================
    275. /**
    276. * 根据key获取Set中的所有值
    277. *
    278. * @param key 键
    279. * @return
    280. */
    281. public Set<Object> sGet(String key) {
    282. try {
    283. return redisTemplate.opsForSet().members(key);
    284. } catch (Exception e) {
    285. e.printStackTrace();
    286. return null;
    287. }
    288. }
    289. /**
    290. * 根据value从一个set中查询,是否存在
    291. *
    292. * @param key 键
    293. * @param value 值
    294. * @return true 存在 false不存在
    295. */
    296. public boolean sHasKey(String key, Object value) {
    297. try {
    298. return redisTemplate.opsForSet().isMember(key, value);
    299. } catch (Exception e) {
    300. e.printStackTrace();
    301. return false;
    302. }
    303. }
    304. /**
    305. * 将数据放入set缓存
    306. *
    307. * @param key 键
    308. * @param values 值 可以是多个
    309. * @return 成功个数
    310. */
    311. public long sSet(String key, Object... values) {
    312. try {
    313. return redisTemplate.opsForSet().add(key, values);
    314. } catch (Exception e) {
    315. e.printStackTrace();
    316. return 0;
    317. }
    318. }
    319. /**
    320. * 将set数据放入缓存
    321. *
    322. * @param key 键
    323. * @param time 时间(秒)
    324. * @param values 值 可以是多个
    325. * @return 成功个数
    326. */
    327. public long sSetAndTime(String key, long time, Object... values) {
    328. try {
    329. Long count = redisTemplate.opsForSet().add(key, values);
    330. if (time > 0) expire(key, time);
    331. return count;
    332. } catch (Exception e) {
    333. e.printStackTrace();
    334. return 0;
    335. }
    336. }
    337. /**
    338. * 获取set缓存的长度
    339. *
    340. * @param key 键
    341. * @return
    342. */
    343. public long sGetSetSize(String key) {
    344. try {
    345. return redisTemplate.opsForSet().size(key);
    346. } catch (Exception e) {
    347. e.printStackTrace();
    348. return 0;
    349. }
    350. }
    351. /**
    352. * 移除值为value的
    353. *
    354. * @param key 键
    355. * @param values 值 可以是多个
    356. * @return 移除的个数
    357. */
    358. public long setRemove(String key, Object... values) {
    359. try {
    360. Long count = redisTemplate.opsForSet().remove(key, values);
    361. return count;
    362. } catch (Exception e) {
    363. e.printStackTrace();
    364. return 0;
    365. }
    366. }
    367. //===============================list=================================
    368. /**
    369. * 获取list缓存的内容
    370. *
    371. * @param key 键
    372. * @param start 开始
    373. * @param end 结束 0 到 -1代表所有值
    374. * @return
    375. */
    376. public List<Object> lGet(String key, long start, long end) {
    377. try {
    378. return redisTemplate.opsForList().range(key, start, end);
    379. } catch (Exception e) {
    380. e.printStackTrace();
    381. return null;
    382. }
    383. }
    384. /**
    385. * 获取list缓存的长度
    386. *
    387. * @param key 键
    388. * @return
    389. */
    390. public long lGetListSize(String key) {
    391. try {
    392. return redisTemplate.opsForList().size(key);
    393. } catch (Exception e) {
    394. e.printStackTrace();
    395. return 0;
    396. }
    397. }
    398. /**
    399. * 通过索引 获取list中的值
    400. *
    401. * @param key 键
    402. * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
    403. * @return
    404. */
    405. public Object lGetIndex(String key, long index) {
    406. try {
    407. return redisTemplate.opsForList().index(key, index);
    408. } catch (Exception e) {
    409. e.printStackTrace();
    410. return null;
    411. }
    412. }
    413. /**
    414. * 将list放入缓存
    415. *
    416. * @param key 键
    417. * @param value 值
    418. * @return
    419. */
    420. public boolean lSet(String key, Object value) {
    421. try {
    422. redisTemplate.opsForList().rightPush(key, value);
    423. return true;
    424. } catch (Exception e) {
    425. e.printStackTrace();
    426. return false;
    427. }
    428. }
    429. /**
    430. * 将list放入缓存
    431. *
    432. * @param key 键
    433. * @param value 值
    434. * @param time 时间(秒)
    435. * @return
    436. */
    437. public boolean lSet(String key, Object value, long time) {
    438. try {
    439. redisTemplate.opsForList().rightPush(key, value);
    440. if (time > 0) expire(key, time);
    441. return true;
    442. } catch (Exception e) {
    443. e.printStackTrace();
    444. return false;
    445. }
    446. }
    447. /**
    448. * 将list放入缓存
    449. *
    450. * @param key 键
    451. * @param value 值
    452. * @return
    453. */
    454. public boolean lSet(String key, List<Object> value) {
    455. try {
    456. redisTemplate.opsForList().rightPushAll(key, value);
    457. return true;
    458. } catch (Exception e) {
    459. e.printStackTrace();
    460. return false;
    461. }
    462. }
    463. /**
    464. * 将list放入缓存
    465. *
    466. * @param key 键
    467. * @param value 值
    468. * @param time 时间(秒)
    469. * @return
    470. */
    471. public boolean lSet(String key, List<Object> value, long time) {
    472. try {
    473. redisTemplate.opsForList().rightPushAll(key, value);
    474. if (time > 0) expire(key, time);
    475. return true;
    476. } catch (Exception e) {
    477. e.printStackTrace();
    478. return false;
    479. }
    480. }
    481. /**
    482. * 根据索引修改list中的某条数据
    483. *
    484. * @param key 键
    485. * @param index 索引
    486. * @param value 值
    487. * @return
    488. */
    489. public boolean lUpdateIndex(String key, long index, Object value) {
    490. try {
    491. redisTemplate.opsForList().set(key, index, value);
    492. return true;
    493. } catch (Exception e) {
    494. e.printStackTrace();
    495. return false;
    496. }
    497. }
    498. /**
    499. * 移除N个值为value
    500. *
    501. * @param key 键
    502. * @param count 移除多少个
    503. * @param value 值
    504. * @return 移除的个数
    505. */
    506. public long lRemove(String key, long count, Object value) {
    507. try {
    508. Long remove = redisTemplate.opsForList().remove(key, count, value);
    509. return remove;
    510. } catch (Exception e) {
    511. e.printStackTrace();
    512. return 0;
    513. }
    514. }
    515. }

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读