当前位置:   article > 正文

SpringBoot整合Redis限制IP频繁访问_springboot 禁止redis ping

springboot 禁止redis ping

编写一个redis工具类

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.redis.connection.DataType;
  3. import org.springframework.data.redis.core.Cursor;
  4. import org.springframework.data.redis.core.ScanOptions;
  5. import org.springframework.data.redis.core.StringRedisTemplate;
  6. import org.springframework.data.redis.core.ZSetOperations;
  7. import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
  8. import org.springframework.stereotype.Component;
  9. import java.util.*;
  10. import java.util.Map.Entry;
  11. import java.util.concurrent.TimeUnit;
  12. @Component
  13. public class RedisUtil {
  14. @Autowired
  15. private StringRedisTemplate redisTemplate;
  16. public void setRedisTemplate(StringRedisTemplate redisTemplate) {
  17. this.redisTemplate = redisTemplate;
  18. }
  19. public StringRedisTemplate getRedisTemplate() {
  20. return this.redisTemplate;
  21. }
  22. /** -------------------key相关操作--------------------- */
  23. /**
  24. * 删除key
  25. *
  26. * @param key
  27. */
  28. public void delete(String key) {
  29. redisTemplate.delete(key);
  30. }
  31. /**
  32. * 批量删除key
  33. *
  34. * @param keys
  35. */
  36. public void delete(Collection<String> keys) {
  37. redisTemplate.delete(keys);
  38. }
  39. /**
  40. * 序列化key
  41. *
  42. * @param key
  43. * @return
  44. */
  45. public byte[] dump(String key) {
  46. return redisTemplate.dump(key);
  47. }
  48. /**
  49. * 是否存在key
  50. *
  51. * @param key
  52. * @return
  53. */
  54. public Boolean hasKey(String key) {
  55. return redisTemplate.hasKey(key);
  56. }
  57. /**
  58. * 设置过期时间
  59. *
  60. * @param key
  61. * @param timeout
  62. * @param unit
  63. * @return
  64. */
  65. public Boolean expire(String key, long timeout, TimeUnit unit) {
  66. return redisTemplate.expire(key, timeout, unit);
  67. }
  68. /**
  69. * 设置过期时间
  70. *
  71. * @param key
  72. * @param date
  73. * @return
  74. */
  75. public Boolean expireAt(String key, Date date) {
  76. return redisTemplate.expireAt(key, date);
  77. }
  78. /**
  79. * 查找匹配的key
  80. *
  81. * @param pattern
  82. * @return
  83. */
  84. public Set<String> keys(String pattern) {
  85. return redisTemplate.keys(pattern);
  86. }
  87. /**
  88. * 将当前数据库的 key 移动到给定的数据库 db 当中
  89. *
  90. * @param key
  91. * @param dbIndex
  92. * @return
  93. */
  94. public Boolean move(String key, int dbIndex) {
  95. return redisTemplate.move(key, dbIndex);
  96. }
  97. /**
  98. * 移除 key 的过期时间,key 将持久保持
  99. *
  100. * @param key
  101. * @return
  102. */
  103. public Boolean persist(String key) {
  104. return redisTemplate.persist(key);
  105. }
  106. /**
  107. * 返回 key 的剩余的过期时间
  108. *
  109. * @param key
  110. * @param unit
  111. * @return
  112. */
  113. public Long getExpire(String key, TimeUnit unit) {
  114. return redisTemplate.getExpire(key, unit);
  115. }
  116. /**
  117. * 返回 key 的剩余的过期时间
  118. *
  119. * @param key
  120. * @return
  121. */
  122. public Long getExpire(String key) {
  123. return redisTemplate.getExpire(key);
  124. }
  125. /**
  126. * 从当前数据库中随机返回一个 key
  127. *
  128. * @return
  129. */
  130. public String randomKey() {
  131. return redisTemplate.randomKey();
  132. }
  133. /**
  134. * 修改 key 的名称
  135. *
  136. * @param oldKey
  137. * @param newKey
  138. */
  139. public void rename(String oldKey, String newKey) {
  140. redisTemplate.rename(oldKey, newKey);
  141. }
  142. /**
  143. * 仅当 newkey 不存在时,将 oldKey 改名为 newkey
  144. *
  145. * @param oldKey
  146. * @param newKey
  147. * @return
  148. */
  149. public Boolean renameIfAbsent(String oldKey, String newKey) {
  150. return redisTemplate.renameIfAbsent(oldKey, newKey);
  151. }
  152. /**
  153. * 返回 key 所储存的值的类型
  154. *
  155. * @param key
  156. * @return
  157. */
  158. public DataType type(String key) {
  159. return redisTemplate.type(key);
  160. }
  161. /** -------------------string相关操作--------------------- */
  162. /**
  163. * 设置指定 key 的值
  164. *
  165. * @param key
  166. * @param value
  167. */
  168. public void set(String key, String value) {
  169. redisTemplate.opsForValue().set(key, value);
  170. }
  171. /**
  172. * 获取指定 key 的值
  173. *
  174. * @param key
  175. * @return
  176. */
  177. public String get(String key) {
  178. return redisTemplate.opsForValue().get(key);
  179. }
  180. /**
  181. * 返回 key 中字符串值的子字符
  182. *
  183. * @param key
  184. * @param start
  185. * @param end
  186. * @return
  187. */
  188. public String getRange(String key, long start, long end) {
  189. return redisTemplate.opsForValue().get(key, start, end);
  190. }
  191. /**
  192. * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
  193. *
  194. * @param key
  195. * @param value
  196. * @return
  197. */
  198. public String getAndSet(String key, String value) {
  199. return redisTemplate.opsForValue().getAndSet(key, value);
  200. }
  201. /**
  202. * 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
  203. *
  204. * @param key
  205. * @param offset
  206. * @return
  207. */
  208. public Boolean getBit(String key, long offset) {
  209. return redisTemplate.opsForValue().getBit(key, offset);
  210. }
  211. /**
  212. * 批量获取
  213. *
  214. * @param keys
  215. * @return
  216. */
  217. public List<String> multiGet(Collection<String> keys) {
  218. return redisTemplate.opsForValue().multiGet(keys);
  219. }
  220. /**
  221. * 设置ASCII码, 字符串'a'的ASCII码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
  222. *
  223. * @param key 位置
  224. * @param value 值,true为1, false为0
  225. * @return
  226. */
  227. public boolean setBit(String key, long offset, boolean value) {
  228. return redisTemplate.opsForValue().setBit(key, offset, value);
  229. }
  230. /**
  231. * 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
  232. *
  233. * @param key
  234. * @param value
  235. * @param timeout 过期时间
  236. * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES
  237. * 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  238. */
  239. public void setEx(String key, String value, long timeout, TimeUnit unit) {
  240. redisTemplate.opsForValue().set(key, value, timeout, unit);
  241. }
  242. /**
  243. * 只有在 key 不存在时设置 key 的值
  244. *
  245. * @param key
  246. * @param value
  247. * @return 之前已经存在返回false, 不存在返回true
  248. */
  249. public boolean setIfAbsent(String key, String value) {
  250. return redisTemplate.opsForValue().setIfAbsent(key, value);
  251. }
  252. /**
  253. * 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
  254. *
  255. * @param key
  256. * @param value
  257. * @param offset 从指定位置开始覆写
  258. */
  259. public void setRange(String key, String value, long offset) {
  260. redisTemplate.opsForValue().set(key, value, offset);
  261. }
  262. /**
  263. * 获取字符串的长度
  264. *
  265. * @param key
  266. * @return
  267. */
  268. public Long size(String key) {
  269. return redisTemplate.opsForValue().size(key);
  270. }
  271. /**
  272. * 批量添加
  273. *
  274. * @param maps
  275. */
  276. public void multiSet(Map<String, String> maps) {
  277. redisTemplate.opsForValue().multiSet(maps);
  278. }
  279. /**
  280. * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
  281. *
  282. * @param maps
  283. * @return 之前已经存在返回false, 不存在返回true
  284. */
  285. public boolean multiSetIfAbsent(Map<String, String> maps) {
  286. return redisTemplate.opsForValue().multiSetIfAbsent(maps);
  287. }
  288. /**
  289. * 增加(自增长), 负数则为自减
  290. *
  291. * @param key
  292. * @return
  293. */
  294. public Long incrBy(String key, long increment) {
  295. return redisTemplate.opsForValue().increment(key, increment);
  296. }
  297. /**
  298. * @param key
  299. * @return
  300. */
  301. public Double incrByFloat(String key, double increment) {
  302. return redisTemplate.opsForValue().increment(key, increment);
  303. }
  304. /**
  305. * 追加到末尾
  306. *
  307. * @param key
  308. * @param value
  309. * @return
  310. */
  311. public Integer append(String key, String value) {
  312. return redisTemplate.opsForValue().append(key, value);
  313. }
  314. /** -------------------hash相关操作------------------------- */
  315. /**
  316. * 获取存储在哈希表中指定字段的值
  317. *
  318. * @param key
  319. * @param field
  320. * @return
  321. */
  322. public Object hGet(String key, String field) {
  323. return redisTemplate.opsForHash().get(key, field);
  324. }
  325. /**
  326. * 获取所有给定字段的值
  327. *
  328. * @param key
  329. * @return
  330. */
  331. public Map<Object, Object> hGetAll(String key) {
  332. return redisTemplate.opsForHash().entries(key);
  333. }
  334. /**
  335. * 获取所有给定字段的值
  336. *
  337. * @param key
  338. * @param fields
  339. * @return
  340. */
  341. public List<Object> hMultiGet(String key, Collection<Object> fields) {
  342. return redisTemplate.opsForHash().multiGet(key, fields);
  343. }
  344. public void hPut(String key, String hashKey, String value) {
  345. redisTemplate.opsForHash().put(key, hashKey, value);
  346. }
  347. public void hPutAll(String key, Map<String, String> maps) {
  348. redisTemplate.opsForHash().putAll(key, maps);
  349. }
  350. /**
  351. * 仅当hashKey不存在时才设置
  352. *
  353. * @param key
  354. * @param hashKey
  355. * @param value
  356. * @return
  357. */
  358. public Boolean hPutIfAbsent(String key, String hashKey, String value) {
  359. return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
  360. }
  361. /**
  362. * 删除一个或多个哈希表字段
  363. *
  364. * @param key
  365. * @param fields
  366. * @return
  367. */
  368. public Long hDelete(String key, Object... fields) {
  369. return redisTemplate.opsForHash().delete(key, fields);
  370. }
  371. /**
  372. * 查看哈希表 key 中,指定的字段是否存在
  373. *
  374. * @param key
  375. * @param field
  376. * @return
  377. */
  378. public boolean hExists(String key, String field) {
  379. return redisTemplate.opsForHash().hasKey(key, field);
  380. }
  381. /**
  382. * 为哈希表 key 中的指定字段的整数值加上增量 increment
  383. *
  384. * @param key
  385. * @param field
  386. * @param increment
  387. * @return
  388. */
  389. public Long hIncrBy(String key, Object field, long increment) {
  390. return redisTemplate.opsForHash().increment(key, field, increment);
  391. }
  392. /**
  393. * 为哈希表 key 中的指定字段的整数值加上增量 increment
  394. *
  395. * @param key
  396. * @param field
  397. * @param delta
  398. * @return
  399. */
  400. public Double hIncrByFloat(String key, Object field, double delta) {
  401. return redisTemplate.opsForHash().increment(key, field, delta);
  402. }
  403. /**
  404. * 获取所有哈希表中的字段
  405. *
  406. * @param key
  407. * @return
  408. */
  409. public Set<Object> hKeys(String key) {
  410. return redisTemplate.opsForHash().keys(key);
  411. }
  412. /**
  413. * 获取哈希表中字段的数量
  414. *
  415. * @param key
  416. * @return
  417. */
  418. public Long hSize(String key) {
  419. return redisTemplate.opsForHash().size(key);
  420. }
  421. /**
  422. * 获取哈希表中所有值
  423. *
  424. * @param key
  425. * @return
  426. */
  427. public List<Object> hValues(String key) {
  428. return redisTemplate.opsForHash().values(key);
  429. }
  430. /**
  431. * 迭代哈希表中的键值对
  432. *
  433. * @param key
  434. * @param options
  435. * @return
  436. */
  437. public Cursor<Entry<Object, Object>> hScan(String key, ScanOptions options) {
  438. return redisTemplate.opsForHash().scan(key, options);
  439. }
  440. /** ------------------------list相关操作---------------------------- */
  441. /**
  442. * 通过索引获取列表中的元素
  443. *
  444. * @param key
  445. * @param index
  446. * @return
  447. */
  448. public String lIndex(String key, long index) {
  449. return redisTemplate.opsForList().index(key, index);
  450. }
  451. /**
  452. * 获取列表指定范围内的元素
  453. *
  454. * @param key
  455. * @param start 开始位置, 0是开始位置
  456. * @param end 结束位置, -1返回所有
  457. * @return
  458. */
  459. public List<String> lRange(String key, long start, long end) {
  460. return redisTemplate.opsForList().range(key, start, end);
  461. }
  462. /**
  463. * 存储在list头部
  464. *
  465. * @param key
  466. * @param value
  467. * @return
  468. */
  469. public Long lLeftPush(String key, String value) {
  470. return redisTemplate.opsForList().leftPush(key, value);
  471. }
  472. /**
  473. * @param key
  474. * @param value
  475. * @return
  476. */
  477. public Long lLeftPushAll(String key, String... value) {
  478. return redisTemplate.opsForList().leftPushAll(key, value);
  479. }
  480. /**
  481. * @param key
  482. * @param value
  483. * @return
  484. */
  485. public Long lLeftPushAll(String key, Collection<String> value) {
  486. return redisTemplate.opsForList().leftPushAll(key, value);
  487. }
  488. /**
  489. * 当list存在的时候才加入
  490. *
  491. * @param key
  492. * @param value
  493. * @return
  494. */
  495. public Long lLeftPushIfPresent(String key, String value) {
  496. return redisTemplate.opsForList().leftPushIfPresent(key, value);
  497. }
  498. /**
  499. * 如果pivot存在,再pivot前面添加
  500. *
  501. * @param key
  502. * @param pivot
  503. * @param value
  504. * @return
  505. */
  506. public Long lLeftPush(String key, String pivot, String value) {
  507. return redisTemplate.opsForList().leftPush(key, pivot, value);
  508. }
  509. /**
  510. * @param key
  511. * @param value
  512. * @return
  513. */
  514. public Long lRightPush(String key, String value) {
  515. return redisTemplate.opsForList().rightPush(key, value);
  516. }
  517. /**
  518. * @param key
  519. * @param value
  520. * @return
  521. */
  522. public Long lRightPushAll(String key, String... value) {
  523. return redisTemplate.opsForList().rightPushAll(key, value);
  524. }
  525. /**
  526. * @param key
  527. * @param value
  528. * @return
  529. */
  530. public Long lRightPushAll(String key, Collection<String> value) {
  531. return redisTemplate.opsForList().rightPushAll(key, value);
  532. }
  533. /**
  534. * 为已存在的列表添加值
  535. *
  536. * @param key
  537. * @param value
  538. * @return
  539. */
  540. public Long lRightPushIfPresent(String key, String value) {
  541. return redisTemplate.opsForList().rightPushIfPresent(key, value);
  542. }
  543. /**
  544. * 在pivot元素的右边添加值
  545. *
  546. * @param key
  547. * @param pivot
  548. * @param value
  549. * @return
  550. */
  551. public Long lRightPush(String key, String pivot, String value) {
  552. return redisTemplate.opsForList().rightPush(key, pivot, value);
  553. }
  554. /**
  555. * 通过索引设置列表元素的值
  556. *
  557. * @param key
  558. * @param index 位置
  559. * @param value
  560. */
  561. public void lSet(String key, long index, String value) {
  562. redisTemplate.opsForList().set(key, index, value);
  563. }
  564. /**
  565. * 移出并获取列表的第一个元素
  566. *
  567. * @param key
  568. * @return 删除的元素
  569. */
  570. public String lLeftPop(String key) {
  571. return redisTemplate.opsForList().leftPop(key);
  572. }
  573. /**
  574. * 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  575. *
  576. * @param key
  577. * @param timeout 等待时间
  578. * @param unit 时间单位
  579. * @return
  580. */
  581. public String lBLeftPop(String key, long timeout, TimeUnit unit) {
  582. return redisTemplate.opsForList().leftPop(key, timeout, unit);
  583. }
  584. /**
  585. * 移除并获取列表最后一个元素
  586. *
  587. * @param key
  588. * @return 删除的元素
  589. */
  590. public String lRightPop(String key) {
  591. return redisTemplate.opsForList().rightPop(key);
  592. }
  593. /**
  594. * 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  595. *
  596. * @param key
  597. * @param timeout 等待时间
  598. * @param unit 时间单位
  599. * @return
  600. */
  601. public String lBRightPop(String key, long timeout, TimeUnit unit) {
  602. return redisTemplate.opsForList().rightPop(key, timeout, unit);
  603. }
  604. /**
  605. * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
  606. *
  607. * @param sourceKey
  608. * @param destinationKey
  609. * @return
  610. */
  611. public String lRightPopAndLeftPush(String sourceKey, String destinationKey) {
  612. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
  613. destinationKey);
  614. }
  615. /**
  616. * 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  617. *
  618. * @param sourceKey
  619. * @param destinationKey
  620. * @param timeout
  621. * @param unit
  622. * @return
  623. */
  624. public String lBRightPopAndLeftPush(String sourceKey, String destinationKey,
  625. long timeout, TimeUnit unit) {
  626. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,
  627. destinationKey, timeout, unit);
  628. }
  629. /**
  630. * 删除集合中值等于value得元素
  631. *
  632. * @param key
  633. * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
  634. * index<0, 从尾部开始删除第一个值等于value的元素;
  635. * @param value
  636. * @return
  637. */
  638. public Long lRemove(String key, long index, String value) {
  639. return redisTemplate.opsForList().remove(key, index, value);
  640. }
  641. /**
  642. * 裁剪list
  643. *
  644. * @param key
  645. * @param start
  646. * @param end
  647. */
  648. public void lTrim(String key, long start, long end) {
  649. redisTemplate.opsForList().trim(key, start, end);
  650. }
  651. /**
  652. * 获取列表长度
  653. *
  654. * @param key
  655. * @return
  656. */
  657. public Long lLen(String key) {
  658. return redisTemplate.opsForList().size(key);
  659. }
  660. /** --------------------set相关操作-------------------------- */
  661. /**
  662. * set添加元素
  663. *
  664. * @param key
  665. * @param values
  666. * @return
  667. */
  668. public Long sAdd(String key, String... values) {
  669. return redisTemplate.opsForSet().add(key, values);
  670. }
  671. /**
  672. * set移除元素
  673. *
  674. * @param key
  675. * @param values
  676. * @return
  677. */
  678. public Long sRemove(String key, Object... values) {
  679. return redisTemplate.opsForSet().remove(key, values);
  680. }
  681. /**
  682. * 移除并返回集合的一个随机元素
  683. *
  684. * @param key
  685. * @return
  686. */
  687. public String sPop(String key) {
  688. return redisTemplate.opsForSet().pop(key);
  689. }
  690. /**
  691. * 将元素value从一个集合移到另一个集合
  692. *
  693. * @param key
  694. * @param value
  695. * @param destKey
  696. * @return
  697. */
  698. public Boolean sMove(String key, String value, String destKey) {
  699. return redisTemplate.opsForSet().move(key, value, destKey);
  700. }
  701. /**
  702. * 获取集合的大小
  703. *
  704. * @param key
  705. * @return
  706. */
  707. public Long sSize(String key) {
  708. return redisTemplate.opsForSet().size(key);
  709. }
  710. /**
  711. * 判断集合是否包含value
  712. *
  713. * @param key
  714. * @param value
  715. * @return
  716. */
  717. public Boolean sIsMember(String key, Object value) {
  718. return redisTemplate.opsForSet().isMember(key, value);
  719. }
  720. /**
  721. * 获取两个集合的交集
  722. *
  723. * @param key
  724. * @param otherKey
  725. * @return
  726. */
  727. public Set<String> sIntersect(String key, String otherKey) {
  728. return redisTemplate.opsForSet().intersect(key, otherKey);
  729. }
  730. /**
  731. * 获取key集合与多个集合的交集
  732. *
  733. * @param key
  734. * @param otherKeys
  735. * @return
  736. */
  737. public Set<String> sIntersect(String key, Collection<String> otherKeys) {
  738. return redisTemplate.opsForSet().intersect(key, otherKeys);
  739. }
  740. /**
  741. * key集合与otherKey集合的交集存储到destKey集合中
  742. *
  743. * @param key
  744. * @param otherKey
  745. * @param destKey
  746. * @return
  747. */
  748. public Long sIntersectAndStore(String key, String otherKey, String destKey) {
  749. return redisTemplate.opsForSet().intersectAndStore(key, otherKey,
  750. destKey);
  751. }
  752. /**
  753. * key集合与多个集合的交集存储到destKey集合中
  754. *
  755. * @param key
  756. * @param otherKeys
  757. * @param destKey
  758. * @return
  759. */
  760. public Long sIntersectAndStore(String key, Collection<String> otherKeys,
  761. String destKey) {
  762. return redisTemplate.opsForSet().intersectAndStore(key, otherKeys,
  763. destKey);
  764. }
  765. /**
  766. * 获取两个集合的并集
  767. *
  768. * @param key
  769. * @param otherKeys
  770. * @return
  771. */
  772. public Set<String> sUnion(String key, String otherKeys) {
  773. return redisTemplate.opsForSet().union(key, otherKeys);
  774. }
  775. /**
  776. * 获取key集合与多个集合的并集
  777. *
  778. * @param key
  779. * @param otherKeys
  780. * @return
  781. */
  782. public Set<String> sUnion(String key, Collection<String> otherKeys) {
  783. return redisTemplate.opsForSet().union(key, otherKeys);
  784. }
  785. /**
  786. * key集合与otherKey集合的并集存储到destKey中
  787. *
  788. * @param key
  789. * @param otherKey
  790. * @param destKey
  791. * @return
  792. */
  793. public Long sUnionAndStore(String key, String otherKey, String destKey) {
  794. return redisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
  795. }
  796. /**
  797. * key集合与多个集合的并集存储到destKey中
  798. *
  799. * @param key
  800. * @param otherKeys
  801. * @param destKey
  802. * @return
  803. */
  804. public Long sUnionAndStore(String key, Collection<String> otherKeys,
  805. String destKey) {
  806. return redisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
  807. }
  808. /**
  809. * 获取两个集合的差集
  810. *
  811. * @param key
  812. * @param otherKey
  813. * @return
  814. */
  815. public Set<String> sDifference(String key, String otherKey) {
  816. return redisTemplate.opsForSet().difference(key, otherKey);
  817. }
  818. /**
  819. * 获取key集合与多个集合的差集
  820. *
  821. * @param key
  822. * @param otherKeys
  823. * @return
  824. */
  825. public Set<String> sDifference(String key, Collection<String> otherKeys) {
  826. return redisTemplate.opsForSet().difference(key, otherKeys);
  827. }
  828. /**
  829. * key集合与otherKey集合的差集存储到destKey中
  830. *
  831. * @param key
  832. * @param otherKey
  833. * @param destKey
  834. * @return
  835. */
  836. public Long sDifference(String key, String otherKey, String destKey) {
  837. return redisTemplate.opsForSet().differenceAndStore(key, otherKey,
  838. destKey);
  839. }
  840. /**
  841. * key集合与多个集合的差集存储到destKey中
  842. *
  843. * @param key
  844. * @param otherKeys
  845. * @param destKey
  846. * @return
  847. */
  848. public Long sDifference(String key, Collection<String> otherKeys,
  849. String destKey) {
  850. return redisTemplate.opsForSet().differenceAndStore(key, otherKeys,
  851. destKey);
  852. }
  853. /**
  854. * 获取集合所有元素
  855. *
  856. * @param key
  857. * @return
  858. */
  859. public Set<String> setMembers(String key) {
  860. return redisTemplate.opsForSet().members(key);
  861. }
  862. /**
  863. * 随机获取集合中的一个元素
  864. *
  865. * @param key
  866. * @return
  867. */
  868. public String sRandomMember(String key) {
  869. return redisTemplate.opsForSet().randomMember(key);
  870. }
  871. /**
  872. * 随机获取集合中count个元素
  873. *
  874. * @param key
  875. * @param count
  876. * @return
  877. */
  878. public List<String> sRandomMembers(String key, long count) {
  879. return redisTemplate.opsForSet().randomMembers(key, count);
  880. }
  881. /**
  882. * 随机获取集合中count个元素并且去除重复的
  883. *
  884. * @param key
  885. * @param count
  886. * @return
  887. */
  888. public Set<String> sDistinctRandomMembers(String key, long count) {
  889. return redisTemplate.opsForSet().distinctRandomMembers(key, count);
  890. }
  891. /**
  892. * @param key
  893. * @param options
  894. * @return
  895. */
  896. public Cursor<String> sScan(String key, ScanOptions options) {
  897. return redisTemplate.opsForSet().scan(key, options);
  898. }
  899. /**------------------zSet相关操作--------------------------------*/
  900. /**
  901. * 添加元素,有序集合是按照元素的score值由小到大排列
  902. *
  903. * @param key
  904. * @param value
  905. * @param score
  906. * @return
  907. */
  908. public Boolean zAdd(String key, String value, double score) {
  909. return redisTemplate.opsForZSet().add(key, value, score);
  910. }
  911. /**
  912. * @param key
  913. * @param values
  914. * @return
  915. */
  916. public Long zAdd(String key, Set<TypedTuple<String>> values) {
  917. return redisTemplate.opsForZSet().add(key, values);
  918. }
  919. /**
  920. * @param key
  921. * @param values
  922. * @return
  923. */
  924. public Long zRemove(String key, Object... values) {
  925. return redisTemplate.opsForZSet().remove(key, values);
  926. }
  927. /**
  928. * 增加元素的score值,并返回增加后的值
  929. *
  930. * @param key
  931. * @param value
  932. * @param delta
  933. * @return
  934. */
  935. public Double zIncrementScore(String key, String value, double delta) {
  936. return redisTemplate.opsForZSet().incrementScore(key, value, delta);
  937. }
  938. /**
  939. * 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
  940. *
  941. * @param key
  942. * @param value
  943. * @return 0表示第一位
  944. */
  945. public Long zRank(String key, Object value) {
  946. return redisTemplate.opsForZSet().rank(key, value);
  947. }
  948. /**
  949. * 返回元素在集合的排名,按元素的score值由大到小排列
  950. *
  951. * @param key
  952. * @param value
  953. * @return
  954. */
  955. public Long zReverseRank(String key, Object value) {
  956. return redisTemplate.opsForZSet().reverseRank(key, value);
  957. }
  958. /**
  959. * 获取集合的元素, 从小到大排序
  960. *
  961. * @param key
  962. * @param start 开始位置
  963. * @param end 结束位置, -1查询所有
  964. * @return
  965. */
  966. public Set<String> zRange(String key, long start, long end) {
  967. return redisTemplate.opsForZSet().range(key, start, end);
  968. }
  969. /**
  970. * 获取集合元素, 并且把score值也获取
  971. *
  972. * @param key
  973. * @param start
  974. * @param end
  975. * @return
  976. */
  977. public Set<TypedTuple<String>> zRangeWithScores(String key, long start,
  978. long end) {
  979. return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
  980. }
  981. /**
  982. * 根据Score值查询集合元素
  983. *
  984. * @param key
  985. * @param min 最小值
  986. * @param max 最大值
  987. * @return
  988. */
  989. public Set<String> zRangeByScore(String key, double min, double max) {
  990. return redisTemplate.opsForZSet().rangeByScore(key, min, max);
  991. }
  992. /**
  993. * 根据Score值查询集合元素, 从小到大排序
  994. *
  995. * @param key
  996. * @param min 最小值
  997. * @param max 最大值
  998. * @return
  999. */
  1000. public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
  1001. double min, double max) {
  1002. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
  1003. }
  1004. /**
  1005. * @param key
  1006. * @param min
  1007. * @param max
  1008. * @param start
  1009. * @param end
  1010. * @return
  1011. */
  1012. public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
  1013. double min, double max, long start, long end) {
  1014. return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
  1015. start, end);
  1016. }
  1017. /**
  1018. * 获取集合的元素, 从大到小排序
  1019. *
  1020. * @param key
  1021. * @param start
  1022. * @param end
  1023. * @return
  1024. */
  1025. public Set<String> zReverseRange(String key, long start, long end) {
  1026. return redisTemplate.opsForZSet().reverseRange(key, start, end);
  1027. }
  1028. /**
  1029. * 获取集合的元素, 从大到小排序, 并返回score值
  1030. *
  1031. * @param key
  1032. * @param start
  1033. * @param end
  1034. * @return
  1035. */
  1036. public Set<TypedTuple<String>> zReverseRangeWithScores(String key,
  1037. long start, long end) {
  1038. return redisTemplate.opsForZSet().reverseRangeWithScores(key, start,
  1039. end);
  1040. }
  1041. /**
  1042. * 根据Score值查询集合元素, 从大到小排序
  1043. *
  1044. * @param key
  1045. * @param min
  1046. * @param max
  1047. * @return
  1048. */
  1049. public Set<String> zReverseRangeByScore(String key, double min,
  1050. double max) {
  1051. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
  1052. }
  1053. /**
  1054. * 根据Score值查询集合元素, 从大到小排序
  1055. *
  1056. * @param key
  1057. * @param min
  1058. * @param max
  1059. * @return
  1060. */
  1061. public Set<ZSetOperations.TypedTuple<String>> zReverseRangeByScoreWithScores(
  1062. String key, double min, double max) {
  1063. return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,
  1064. min, max);
  1065. }
  1066. /**
  1067. * @param key
  1068. * @param min
  1069. * @param max
  1070. * @param start
  1071. * @param end
  1072. * @return
  1073. */
  1074. public Set<String> zReverseRangeByScore(String key, double min,
  1075. double max, long start, long end) {
  1076. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max,
  1077. start, end);
  1078. }
  1079. /**
  1080. * 根据score值获取集合元素数量
  1081. *
  1082. * @param key
  1083. * @param min
  1084. * @param max
  1085. * @return
  1086. */
  1087. public Long zCount(String key, double min, double max) {
  1088. return redisTemplate.opsForZSet().count(key, min, max);
  1089. }
  1090. /**
  1091. * 获取集合大小
  1092. *
  1093. * @param key
  1094. * @return
  1095. */
  1096. public Long zSize(String key) {
  1097. return redisTemplate.opsForZSet().size(key);
  1098. }
  1099. /**
  1100. * 获取集合大小
  1101. *
  1102. * @param key
  1103. * @return
  1104. */
  1105. public Long zZCard(String key) {
  1106. return redisTemplate.opsForZSet().zCard(key);
  1107. }
  1108. /**
  1109. * 获取集合中value元素的score值
  1110. *
  1111. * @param key
  1112. * @param value
  1113. * @return
  1114. */
  1115. public Double zScore(String key, Object value) {
  1116. return redisTemplate.opsForZSet().score(key, value);
  1117. }
  1118. /**
  1119. * 移除指定索引位置的成员
  1120. *
  1121. * @param key
  1122. * @param start
  1123. * @param end
  1124. * @return
  1125. */
  1126. public Long zRemoveRange(String key, long start, long end) {
  1127. return redisTemplate.opsForZSet().removeRange(key, start, end);
  1128. }
  1129. /**
  1130. * 根据指定的score值的范围来移除成员
  1131. *
  1132. * @param key
  1133. * @param min
  1134. * @param max
  1135. * @return
  1136. */
  1137. public Long zRemoveRangeByScore(String key, double min, double max) {
  1138. return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
  1139. }
  1140. /**
  1141. * 获取key和otherKey的并集并存储在destKey中
  1142. *
  1143. * @param key
  1144. * @param otherKey
  1145. * @param destKey
  1146. * @return
  1147. */
  1148. public Long zUnionAndStore(String key, String otherKey, String destKey) {
  1149. return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
  1150. }
  1151. /**
  1152. * @param key
  1153. * @param otherKeys
  1154. * @param destKey
  1155. * @return
  1156. */
  1157. public Long zUnionAndStore(String key, Collection<String> otherKeys,
  1158. String destKey) {
  1159. return redisTemplate.opsForZSet()
  1160. .unionAndStore(key, otherKeys, destKey);
  1161. }
  1162. /**
  1163. * 交集
  1164. *
  1165. * @param key
  1166. * @param otherKey
  1167. * @param destKey
  1168. * @return
  1169. */
  1170. public Long zIntersectAndStore(String key, String otherKey,
  1171. String destKey) {
  1172. return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
  1173. destKey);
  1174. }
  1175. /**
  1176. * 交集
  1177. *
  1178. * @param key
  1179. * @param otherKeys
  1180. * @param destKey
  1181. * @return
  1182. */
  1183. public Long zIntersectAndStore(String key, Collection<String> otherKeys,
  1184. String destKey) {
  1185. return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
  1186. destKey);
  1187. }
  1188. /**
  1189. * @param key
  1190. * @param options
  1191. * @return
  1192. */
  1193. public Cursor<TypedTuple<String>> zScan(String key, ScanOptions options) {
  1194. return redisTemplate.opsForZSet().scan(key, options);
  1195. }
  1196. }

 

 

在Springboot上编写一个过滤器

  1. @Slf4j
  2. @Order(0)
  3. @Component
  4. @WebFilter(filterName = "IPFilter", urlPatterns = "/*")
  5. public class IPFilter implements Filter {
  6. //单位时间内最大访问数
  7. private static final Integer MAX_COUNT = 15;
  8. //单位时间
  9. private static final Integer UNIT_TIME = 1 * 1000;
  10. //限制时长
  11. private static final Long REJECT_TIME = 10 * 60 * 1000L;
  12. private static RedisUtil redisUtil;
  13. private ApplicationContext context;
  14. @Override
  15. public void init(FilterConfig filterConfig) throws ServletException {
  16. context = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
  17. if (redisUtil == null) {
  18. redisUtil = new RedisUtil();
  19. StringRedisTemplate redisTemplate = (StringRedisTemplate) context.getBean("stringRedisTemplate");
  20. redisUtil.setRedisTemplate(redisTemplate);
  21. }
  22. }
  23. @Override
  24. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  25. String ip = request.getRemoteAddr();
  26. //过滤黑名单
  27. if (redisUtil.hasKey("filter:ip:black:" + ip)) {
  28. log.error("ip访问过于频繁,已被限制=>" + ip + " 倒计时" + redisUtil.getExpire("filter:ip:black:" + ip));
  29. return;
  30. }
  31. //判断ip是否首次访问
  32. if (redisUtil.hExists("filter:ip:normal", "count:" + ip)) {
  33. //判断最大访问次数
  34. Integer maxCount = Integer.valueOf(redisUtil.hGet("filter:ip:normal", "count:" + ip).toString());
  35. log.info("ip:" + ip + " 访问" + maxCount + "次");
  36. if (maxCount > MAX_COUNT) {
  37. Long maxTime = Long.valueOf(redisUtil.hGet("filter:ip:normal", "time:" + ip).toString());
  38. if (System.currentTimeMillis() - maxTime < UNIT_TIME) {
  39. log.error("ip访问过于频繁,已被限制=>" + ip + " 倒计时" + REJECT_TIME);
  40. redisUtil.setEx("filter:ip:black:" + ip, "1", REJECT_TIME, TimeUnit.MILLISECONDS);
  41. String str[] = {"count:" + ip, "time:" + ip};
  42. redisUtil.hDelete("filter:ip:normal", str);
  43. return;
  44. }
  45. initVisitsIP(ip);
  46. }
  47. } else {
  48. initVisitsIP(ip);
  49. }
  50. chain.doFilter(request, response);
  51. redisUtil.hIncrBy("filter:ip:normal", "count:" + ip, 1);
  52. }
  53. /**
  54. * 初始化访问ip
  55. * @param ip
  56. */
  57. private void initVisitsIP(String ip) {
  58. redisUtil.hPut("filter:ip:normal", "count:" + ip, "0");
  59. redisUtil.hPut("filter:ip:normal", "time:" + ip, String.valueOf(System.currentTimeMillis()));
  60. }
  61. @Override
  62. public void destroy() {
  63. }

 

 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号