Redis操作字符串工具类封装,Redis工具类封装

Java代码  
  1. package com.lqy.spring.redis;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. public class RedisPoolUtils {
  5. private static final JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
  6. //spring配置使用
  7. //private static final JedisPool jedisPool = (JedisPool) SpringUtils.getBeanById("jedisPool");
  8. /**
  9. * 成功,"OK"
  10. */
  11. private static final String SUCCESS_OK = "OK";
  12. /**
  13. * 成功,1L
  14. */
  15. private static final Long SUCCESS_STATUS_LONG = 1L;
  16. /**
  17. * 只用key不存在时才设置。Only set the key if it does not already exist
  18. */
  19. private static final String NX = "NX";
  20. /**
  21. * XX -- 只有key存在时才设置。和NX相反。Only set the key if it already exist.
  22. */
  23. private static final String XX = "XX";
  24. /**
  25. * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
  26. */
  27. private static final String EX = "EX";
  28. /**
  29. * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
  30. */
  31. //private static final String PX = "PX";
  32. /**
  33. * 成功返回true
  34. * @param key
  35. * @param value
  36. * @return
  37. */
  38. public static boolean set(String key, String value){
  39. Jedis jedis = null;
  40. try {
  41. jedis = jedisPool.getResource();
  42. String statusCode = jedis.set(key, value);
  43. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  44. return true;
  45. }
  46. }catch (Exception e) {
  47. e.printStackTrace();
  48. }finally{
  49. if(jedis != null){
  50. jedis.close();
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * 返回值
  57. * @param key
  58. * @return
  59. */
  60. public static String get(String key){
  61. Jedis jedis = null;
  62. try {
  63. jedis = jedisPool.getResource();
  64. return jedis.get(key);
  65. }catch (Exception e) {
  66. e.printStackTrace();
  67. }finally{
  68. if(jedis != null){
  69. jedis.close();
  70. }
  71. }
  72. return null;
  73. }
  74. /**
  75. * 设置key值和过期时间
  76. * @param key
  77. * @param value
  78. * @param seconds 秒数,不能小于0
  79. * @return
  80. */
  81. public static boolean setByTime(String key, String value, int seconds){
  82. if(seconds < 0){
  83. return false;
  84. }
  85. Jedis jedis = null;
  86. try {
  87. jedis = jedisPool.getResource();
  88. String statusCode = jedis.setex(key, seconds, value);
  89. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  90. return true;
  91. }
  92. }catch (Exception e) {
  93. e.printStackTrace();
  94. }finally{
  95. if(jedis != null){
  96. jedis.close();
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. *
  103. * @param key
  104. * @param value
  105. * @param nxxx NX|XX  是否存在
  106. * <li>NX -- Only set the key if it does not already exist.</li>
  107. * <li>XX -- Only set the key if it already exist.</li>
  108. * @param expx EX|PX, expire time units ,时间单位格式,秒或毫秒
  109. * <li>EX = seconds;</li>
  110. * <li>PX = milliseconds</li>
  111. * @param time expire time in the units of expx,时间(long型),不能小于0
  112. * @return
  113. */
  114. public static boolean set(String key, String value,
  115. String nxxx, String expx, long time){
  116. if(time < 0){
  117. return false;
  118. }
  119. Jedis jedis = null;
  120. try {
  121. jedis = jedisPool.getResource();
  122. String statusCode = jedis.set(key, value, nxxx, expx, time);
  123. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  124. return true;
  125. }
  126. }catch (Exception e) {
  127. e.printStackTrace();
  128. }finally{
  129. if(jedis != null){
  130. jedis.close();
  131. }
  132. }
  133. return false;
  134. }
  135. /**
  136. * 设置key
  137. * @param key
  138. * @param value
  139. * @param nxxx NX|XX 是否需要存在
  140. * <li>NX -- Only set the key if it does not already exist.</li>
  141. * <li>XX -- Only set the key if it already exist.</li>
  142. * @return
  143. */
  144. public static boolean set(String key, String value,
  145. String nxxx){
  146. Jedis jedis = null;
  147. try {
  148. jedis = jedisPool.getResource();
  149. String statusCode = jedis.set(key, value, nxxx);
  150. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  151. return true;
  152. }
  153. }catch (Exception e) {
  154. e.printStackTrace();
  155. }finally{
  156. if(jedis != null){
  157. jedis.close();
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * 当key不存在时,设置值,成功返回true
  164. * @param key
  165. * @param value
  166. * @return
  167. */
  168. public static boolean setIfNotExists(String key, String value){
  169. Jedis jedis = null;
  170. try {
  171. jedis = jedisPool.getResource();
  172. Long statusCode = jedis.setnx(key, value);
  173. if(SUCCESS_STATUS_LONG == statusCode){
  174. return true;
  175. }
  176. }catch (Exception e) {
  177. e.printStackTrace();
  178. }finally{
  179. if(jedis != null){
  180. jedis.close();
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * 当key不存在时,设置值,成功返回true,同setIfNotExists
  187. * @param key
  188. * @param value
  189. * @return
  190. */
  191. public static boolean setNX(String key, String value){
  192. return setIfNotExists(key, value);
  193. }
  194. /**
  195. * 仅当key不存在时则设置值,成功返回true,存在时不设置值
  196. * @param key
  197. * @param value
  198. * @param seconds,秒数,不能小于0
  199. * @return
  200. */
  201. public static boolean setIfNotExists(String key, String value, long seconds){
  202. if(seconds < 0){
  203. return false;
  204. }
  205. return set(key, value, NX, EX, seconds);
  206. }
  207. /**
  208. * 仅当key不存在时则设置值,成功返回true,存在时不设置值,同setIfNotExists(key, value, seconds)
  209. * @param key
  210. * @param value
  211. * @param seconds
  212. * @return
  213. */
  214. public static boolean setNX(String key, String value, Long seconds){
  215. return setIfNotExists(key, value, seconds);
  216. }
  217. /**
  218. * 当key存在时则设置值,成功返回true,不存在不设置值
  219. * @param key
  220. * @param value
  221. * @return
  222. */
  223. public static boolean setIfExists(String key, String value){
  224. return set(key, value, XX);
  225. }
  226. /**
  227. * 当key存在时则设置值,成功返回true,不存在不设置值,同setIfExists
  228. * @param key
  229. * @param value
  230. * @return
  231. */
  232. public static boolean setXX(String key, String value){
  233. return setIfExists(key, value);
  234. }
  235. /**
  236. * 仅当key存在时则设置值,成功返回true,不存在不设置值
  237. * @param key
  238. * @param value
  239. * @param seconds,秒数,不能小于0
  240. * @return
  241. */
  242. public static boolean setIfExists(String key, String value, long seconds){
  243. if(seconds < 0){
  244. return false;
  245. }
  246. return set(key, value, XX, EX, seconds);
  247. }
  248. /**
  249. * 仅当key存在时则设置值,成功返回true,不存在不设置值
  250. * @param key
  251. * @param value
  252. * @param seconds,秒数,不能小于0
  253. * @return
  254. */
  255. public static boolean setXX(String key, String value, long seconds){
  256. return setIfExists(key, value, seconds);
  257. }
  258. /**
  259. * 设置超期时间
  260. * @param key
  261. * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期
  262. * @return
  263. */
  264. public static boolean setTime(String key, Integer seconds){
  265. Jedis jedis = null;
  266. try {
  267. if(seconds == null){
  268. seconds = -1;
  269. }
  270. jedis = jedisPool.getResource();
  271. Long statusCode = jedis.expire(key, seconds);
  272. if(SUCCESS_STATUS_LONG == statusCode){
  273. return true;
  274. }
  275. }catch (Exception e) {
  276. e.printStackTrace();
  277. }finally{
  278. if(jedis != null){
  279. jedis.close();
  280. }
  281. }
  282. return false;
  283. }
  284. /**
  285. * 设置超期时间
  286. * @param key
  287. * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期
  288. * @return
  289. */
  290. public static boolean setOutTime(String key, Integer seconds){
  291. return setTime(key, seconds);
  292. }
  293. /**
  294. * 设置超期时间
  295. * @param key
  296. * @param seconds 秒数,为Null时,将会马上过期。可以设置-1,0,表示马上过期
  297. * @return
  298. */
  299. public static boolean expire(String key, Integer seconds){
  300. return setTime(key, seconds);
  301. }
  302. /**
  303. * 判断key是否存在,存在返回true
  304. * @param key
  305. * @return
  306. */
  307. public static boolean exists(String key){
  308. Jedis jedis = null;
  309. try {
  310. jedis = jedisPool.getResource();
  311. return jedis.exists(key);
  312. }catch (Exception e) {
  313. e.printStackTrace();
  314. }finally{
  315. if(jedis != null){
  316. jedis.close();
  317. }
  318. }
  319. return false;
  320. }
  321. /**
  322. * 判断key是否存在,存在返回true
  323. * @param key
  324. * @return
  325. */
  326. public static boolean isExists(String key){
  327. return exists(key);
  328. }
  329. /**
  330. * 将key设置为永久
  331. * @param key
  332. * @return
  333. */
  334. public static boolean persist(String key){
  335. Jedis jedis = null;
  336. try {
  337. jedis = jedisPool.getResource();
  338. long time = getTime(key);
  339. if(time == -1){
  340. return true;
  341. }
  342. //已经是永久的,返回0
  343. Long statusCode = jedis.persist(key);
  344. jedis.close();
  345. if(SUCCESS_STATUS_LONG == statusCode){
  346. return true;
  347. }
  348. }catch (Exception e) {
  349. e.printStackTrace();
  350. }finally{
  351. if(jedis != null){
  352. jedis.close();
  353. }
  354. }
  355. return false;
  356. }
  357. /**
  358. * 获取剩余时间(秒)
  359. * @param key
  360. * @return
  361. */
  362. public static Long getTime(String key){
  363. Jedis jedis = null;
  364. try {
  365. jedis = jedisPool.getResource();
  366. return jedis.ttl(key);
  367. }catch (Exception e) {
  368. e.printStackTrace();
  369. }finally{
  370. if(jedis != null){
  371. jedis.close();
  372. }
  373. }
  374. return -1L;
  375. }
  376. /**
  377. * 获取剩余时间(秒)
  378. * @param key
  379. * @return
  380. */
  381. public static Long Ttl(String key){
  382. return getTime(key);
  383. }
  384. /**
  385. * 随机获取一个key
  386. * @return
  387. */
  388. public static String randomKey(){
  389. Jedis jedis = null;
  390. try {
  391. jedis = jedisPool.getResource();
  392. return jedis.randomKey();
  393. }catch (Exception e) {
  394. e.printStackTrace();
  395. }finally{
  396. if(jedis != null){
  397. jedis.close();
  398. }
  399. }
  400. return null;
  401. }
  402. /**
  403. * 随机获取一个key
  404. * @return
  405. */
  406. public static String random(){
  407. return randomKey();
  408. }
  409. /**
  410. * 修改 key 的名称,成功返回true,如果不存在该key,则会抛错:ERR no such key
  411. * 注:如果newKey已经存在,则会进行覆盖。建议使用renameNX
  412. * @param oldkey 原来的key
  413. * @param newKey 新的key
  414. * @return
  415. */
  416. public static boolean rename(String oldkey, String newKey){
  417. Jedis jedis = null;
  418. try {
  419. jedis = jedisPool.getResource();
  420. String statusCode = jedis.rename(oldkey, newKey);
  421. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  422. return true;
  423. }
  424. }catch (Exception e) {
  425. e.printStackTrace();
  426. }finally{
  427. if(jedis != null){
  428. jedis.close();
  429. }
  430. }
  431. return false;
  432. }
  433. /**
  434. * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true
  435. * @param oldkey
  436. * @param newKey
  437. * @return
  438. */
  439. public static boolean renameNX(String oldkey, String newKey){
  440. Jedis jedis = null;
  441. try {
  442. jedis = jedisPool.getResource();
  443. Long statusCode = jedis.renamenx(oldkey, newKey);
  444. if(SUCCESS_STATUS_LONG == statusCode){
  445. return true;
  446. }
  447. }catch (Exception e) {
  448. e.printStackTrace();
  449. }finally{
  450. if(jedis != null){
  451. jedis.close();
  452. }
  453. }
  454. return false;
  455. }
  456. /**
  457. * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true
  458. * @param oldkey
  459. * @param newKey
  460. * @return
  461. */
  462. public static boolean renameIfNotExists(String oldkey, String newKey){
  463. return renameNX(oldkey, newKey);
  464. }
  465. /**
  466. * 返回 key 所储存的值的类型。
  467. * @param key
  468. * @return
  469. */
  470. public static String type(String key){
  471. Jedis jedis = null;
  472. try {
  473. jedis = jedisPool.getResource();
  474. return jedis.type(key);
  475. }catch (Exception e) {
  476. e.printStackTrace();
  477. }finally{
  478. if(jedis != null){
  479. jedis.close();
  480. }
  481. }
  482. return null;
  483. }
  484. /**
  485. * 返回 key 所储存的值的类型。
  486. * @param key
  487. * @return
  488. */
  489. public static String getType(String key){
  490. return type(key);
  491. }
  492. /**
  493. * 删除key及值
  494. * @param key
  495. * @return
  496. */
  497. public static boolean del(String key){
  498. Jedis jedis = null;
  499. try {
  500. jedis = jedisPool.getResource();
  501. Long statusCode = jedis.del(key);
  502. if(SUCCESS_STATUS_LONG == statusCode){
  503. return true;
  504. }
  505. }catch (Exception e) {
  506. e.printStackTrace();
  507. }finally{
  508. if(jedis != null){
  509. jedis.close();
  510. }
  511. }
  512. return false;
  513. }
  514. /**
  515. * 删除key及值
  516. * @param key
  517. * @return
  518. */
  519. public static boolean delete(String key){
  520. return del(key);
  521. }
  522. /**
  523. * 删除key及值
  524. * @param key
  525. * @return
  526. */
  527. public static boolean remove(String key){
  528. return del(key);
  529. }
  530. /**
  531. * 批量删除key及值
  532. * @param key
  533. * @return
  534. */
  535. public static boolean del(String[] keys){
  536. Jedis jedis = null;
  537. try {
  538. jedis = jedisPool.getResource();
  539. Long statusCode = jedis.del(keys);
  540. if(statusCode > 0){
  541. return true;
  542. }
  543. }catch (Exception e) {
  544. e.printStackTrace();
  545. }finally{
  546. if(jedis != null){
  547. jedis.close();
  548. }
  549. }
  550. return false;
  551. }
  552. /**
  553. * 批量删除key及值
  554. * @param key
  555. * @return
  556. */
  557. public static boolean delete(String[] keys){
  558. return del(keys);
  559. }
  560. /**
  561. * 批量删除key及值
  562. * @param key
  563. * @return
  564. */
  565. public static boolean remove(String[] keys){
  566. return del(keys);
  567. }
  568. public static void main(String[] args) {
  569. //System.out.println("statusCode="+statusCode);
  570. //System.out.println(set("wahaha1", "哇哈哈"));
  571. //System.out.println(setByTime("wahaha1", "哈哈", 1));
  572. //System.out.println(getTime("wahaha1"));
  573. /*System.out.println(set("wa", "哈哈", NX, EX, 10L));
  574. System.out.println(set("wa", "哈哈60", XX, EX, 60L));*/
  575. //System.out.println(set("wa", "哈哈哈哈2", XX));
  576. //System.out.println(setIfNotExists("wa", "哈哈not"));
  577. //System.out.println(setIfNotExists("wa", "哈哈not", 30));
  578. //System.out.println(setIfExists("wahaha", "有就设置"));
  579. //System.out.println(setIfExists("wahaha", "有就设置", 60));
  580. //System.out.println(setTime("wa", -1));
  581. //System.out.println(exists("wa"));
  582. //System.out.println(isExists("wa"));
  583. //System.out.println(setByTime("wa", "30秒过期", 30));
  584. //System.out.println(persist("wa"));
  585. /*for(int i=0; i<30; i++){
  586. System.out.println(randomKey());
  587. }*/
  588. //System.out.println(rename("waa", "wa"));
  589. //System.out.println(renameNX("waa", "waa"));
  590. //System.out.println(getType("wa"));
  591. /*System.out.println(del("wa"));
  592. System.out.println(get("wa"));
  593. System.out.println(Ttl("wa"));*/
  594. System.out.println(del(new String[]{"a"}));
  595. }
  596. }

Redis操作Hash工具类封装,Redis工具类封装

Java代码  
  1. /**************************** redis Hash start***************************/
  2. /***Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。***/
  3. /**
  4. * 设置Hash的属性
  5. * @param key
  6. * @param field
  7. * @param value
  8. * @return
  9. */
  10. public static boolean hset(String key, String field, String value){
  11. if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
  12. return false;
  13. }
  14. Jedis jedis = jedisPool.getResource();
  15. //If the field already exists, and the HSET just produced an update of the value, 0 is returned,
  16. //otherwise if a new field is created 1 is returned.
  17. Long statusCode = jedis.hset(key, field, value);
  18. jedis.close();
  19. if(statusCode > -1){
  20. return true;
  21. }
  22. return false;
  23. }
  24. /**
  25. * 批量设置Hash的属性
  26. * @param key
  27. * @param fields
  28. * @param values
  29. * @return
  30. */
  31. public static boolean hmset(String key, String[] fields, String[] values){
  32. if(StrUtils.isBlank(key) || StrUtils.isEmptyArray(fields) || StrUtils.isEmptyArray(values)){
  33. return false;
  34. }
  35. Jedis jedis = jedisPool.getResource();
  36. Map<String, String> hash = new HashMap<String, String>();
  37. for (int i=0; i<fields.length; i++) {
  38. hash.put(fields[i], values[i]);
  39. }
  40. String statusCode = jedis.hmset(key, hash);
  41. jedis.close();
  42. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * 批量设置Hash的属性
  49. * @param key
  50. * @param map Map<String, String>
  51. * @return
  52. */
  53. public static boolean hmset(String key, Map<String, String> map){
  54. if(StrUtils.isBlank(key) || StrUtils.isEmptyMap(map)){
  55. return false;
  56. }
  57. Jedis jedis = jedisPool.getResource();
  58. String statusCode = jedis.hmset(key, map);
  59. jedis.close();
  60. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * 仅当field不存在时设置值,成功返回true
  67. * @param key
  68. * @param field
  69. * @param value
  70. * @return
  71. */
  72. public static boolean hsetNX(String key, String field, String value){
  73. if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
  74. return false;
  75. }
  76. Jedis jedis = jedisPool.getResource();
  77. //If the field already exists, 0 is returned,
  78. //otherwise if a new field is created 1 is returned.
  79. Long statusCode = jedis.hsetnx(key, field, value);
  80. jedis.close();
  81. if(SUCCESS_STATUS_LONG == statusCode){
  82. return true;
  83. }
  84. return false;
  85. }
  86. /**
  87. * 获取属性的值
  88. * @param key
  89. * @param field
  90. * @return
  91. */
  92. public static String hget(String key, String field){
  93. if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
  94. return null;
  95. }
  96. Jedis jedis = jedisPool.getResource();
  97. String value = jedis.hget(key, field);
  98. jedis.close();
  99. return value;
  100. }
  101. /**
  102. * 批量获取属性的值
  103. * @param key
  104. * @param fields String...
  105. * @return
  106. */
  107. public static List<String> hmget(String key, String... fields){
  108. if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){
  109. return null;
  110. }
  111. Jedis jedis = jedisPool.getResource();
  112. List<String> values = jedis.hmget(key, fields);
  113. jedis.close();
  114. return values;
  115. }
  116. /**
  117. * 获取在哈希表中指定 key 的所有字段和值
  118. * @param key
  119. * @return Map<String, String>
  120. */
  121. public static Map<String, String> hgetAll(String key){
  122. if(StrUtils.isBlank(key)){
  123. return null;
  124. }
  125. Jedis jedis = jedisPool.getResource();
  126. Map<String, String> map = jedis.hgetAll(key);
  127. jedis.close();
  128. return map;
  129. }
  130. /**
  131. * 删除hash的属性
  132. * @param key
  133. * @param fields
  134. * @return
  135. */
  136. public static boolean hdel(String key, String... fields){
  137. if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){
  138. return false;
  139. }
  140. Jedis jedis = jedisPool.getResource();
  141. jedis.hdel(key, fields);
  142. jedis.close();
  143. //System.out.println("statusCode="+statusCode);
  144. return true;
  145. }
  146. /**
  147. * 查看哈希表 key 中,指定的字段是否存在。
  148. * @param key
  149. * @param field
  150. * @return
  151. */
  152. public static boolean hexists(String key, String field){
  153. if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){
  154. return false;
  155. }
  156. Jedis jedis = jedisPool.getResource();
  157. boolean result = jedis.hexists(key, field);
  158. jedis.close();
  159. return result;
  160. }
  161. /**
  162. * 为哈希表 key 中的指定字段的整数值加上增量 increment 。
  163. * @param key
  164. * @param field
  165. * @param increment 正负数、0、正整数
  166. * @return
  167. */
  168. public static long hincrBy(String key, String field, long increment){
  169. Jedis jedis = jedisPool.getResource();
  170. long result = jedis.hincrBy(key, field, increment);
  171. jedis.close();
  172. return result;
  173. }
  174. /**
  175. * 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。(注:如果field不存在时,会设置新的值)
  176. * @param key
  177. * @param field
  178. * @param increment,可以为负数、正数、0
  179. * @return
  180. */
  181. public static Double hincrByFloat(String key, String field, double increment){
  182. Jedis jedis = jedisPool.getResource();
  183. Double result = jedis.hincrByFloat(key, field, increment);
  184. jedis.close();
  185. return result;
  186. }
  187. /**
  188. * 获取所有哈希表中的字段
  189. * @param key
  190. * @return Set<String>
  191. */
  192. public static Set<String> hkeys(String key){
  193. Jedis jedis = jedisPool.getResource();
  194. Set<String> result = jedis.hkeys(key);
  195. jedis.close();
  196. return result;
  197. }
  198. /**
  199. * 获取哈希表中所有值
  200. * @param key
  201. * @return List<String>
  202. */
  203. public static List<String> hvals(String key){
  204. Jedis jedis = jedisPool.getResource();
  205. List<String> result = jedis.hvals(key);
  206. jedis.close();
  207. return result;
  208. }
  209. /**
  210. * 获取哈希表中字段的数量,当key不存在时,返回0
  211. * @param key
  212. * @return
  213. */
  214. public static Long hlen(String key){
  215. Jedis jedis = jedisPool.getResource();
  216. Long result = jedis.hlen(key);
  217. jedis.close();
  218. return result;
  219. }
  220. /**
  221. * 迭代哈希表中的键值对。
  222. * @param key
  223. * @param cursor
  224. * @return ScanResult<Entry<String, String>>
  225. */
  226. public static ScanResult<Entry<String, String>> hscan(String key, String cursor){
  227. Jedis jedis = jedisPool.getResource();
  228. ScanResult<Entry<String, String>> scanResult = jedis.hscan(key, cursor);
  229. jedis.close();
  230. //System.out.println(scanResult.getResult());
  231. return scanResult;
  232. }
  233. /**************************** redis Hash end***************************/

Redis操作List工具类封装,Java Redis List命令封装

Java代码  
  1. /**************************** redis 列表List start***************************/
  2. /**
  3. * 将一个值插入到列表头部,value可以重复,返回列表的长度
  4. * @param key
  5. * @param value String
  6. * @return 返回List的长度
  7. */
  8. public static Long lpush(String key, String value){
  9. Jedis jedis = jedisPool.getResource();
  10. Long length = jedis.lpush(key, value);
  11. jedis.close();
  12. return length;
  13. }
  14. /**
  15. * 将多个值插入到列表头部,value可以重复
  16. * @param key
  17. * @param values String[]
  18. * @return 返回List的数量size
  19. */
  20. public static Long lpush(String key, String[] values){
  21. Jedis jedis = jedisPool.getResource();
  22. Long size = jedis.lpush(key, values);
  23. jedis.close();
  24. //System.out.println(result);
  25. return size;
  26. }
  27. /**
  28. * 获取List列表
  29. * @param key
  30. * @param start long,开始索引
  31. * @param end long, 结束索引
  32. * @return List<String>
  33. */
  34. public static List<String> lrange(String key, long start, long end){
  35. Jedis jedis = jedisPool.getResource();
  36. List<String> list = jedis.lrange(key, start, end);
  37. jedis.close();
  38. return list;
  39. }
  40. /**
  41. * 通过索引获取列表中的元素
  42. * @param key
  43. * @param index,索引,0表示最新的一个元素
  44. * @return String
  45. */
  46. public static String lindex(String key, long index){
  47. Jedis jedis = jedisPool.getResource();
  48. String str = jedis.lindex(key, index);
  49. jedis.close();
  50. return str;
  51. }
  52. /**
  53. * 获取列表长度,key为空时返回0
  54. * @param key
  55. * @return Long
  56. */
  57. public static Long llen(String key){
  58. Jedis jedis = jedisPool.getResource();
  59. Long length = jedis.llen(key);
  60. jedis.close();
  61. return length;
  62. }
  63. /**
  64. * 在列表的元素前或者后插入元素,返回List的长度
  65. * @param key
  66. * @param where LIST_POSITION
  67. * @param pivot 以该元素作为参照物,是在它之前,还是之后(pivot:枢轴;中心点,中枢;[物]支点,支枢;[体]回转运动。)
  68. * @param value
  69. * @return Long
  70. */
  71. public static Long linsert(String key, LIST_POSITION where, String pivot, String value){
  72. Jedis jedis = jedisPool.getResource();
  73. Long length = jedis.linsert(key, where, pivot, value);
  74. jedis.close();
  75. return length;
  76. }
  77. /**
  78. * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0)
  79. * @param key
  80. * @param value String
  81. * @return Long
  82. */
  83. public static Long lpushx(String key, String value){
  84. Jedis jedis = jedisPool.getResource();
  85. Long length = jedis.lpushx(key, value);
  86. jedis.close();
  87. return length;
  88. }
  89. /**
  90. * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0)
  91. * @param key
  92. * @param values String[]
  93. * @return Long
  94. */
  95. public static Long lpushx(String key, String[] values){
  96. Jedis jedis = jedisPool.getResource();
  97. Long length = jedis.lpushx(key, values);
  98. jedis.close();
  99. return length;
  100. }
  101. /**
  102. * 移除列表元素,返回移除的元素数量
  103. * @param key
  104. * @param count,标识,表示动作或者查找方向
  105. * <li>当count=0时,移除所有匹配的元素;</li>
  106. * <li>当count为负数时,移除方向是从尾到头;</li>
  107. * <li>当count为正数时,移除方向是从头到尾;</li>
  108. * @param value 匹配的元素
  109. * @return Long
  110. */
  111. public static Long lrem(String key, long count, String value){
  112. Jedis jedis = jedisPool.getResource();
  113. Long length = jedis.lrem(key, count, value);
  114. jedis.close();
  115. return length;
  116. }
  117. /**
  118. * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true
  119. * @param key
  120. * @param index 索引
  121. * @param value
  122. * @return boolean
  123. */
  124. public static boolean lset(String key, long index, String value){
  125. Jedis jedis = jedisPool.getResource();
  126. String statusCode = jedis.lset(key, index, value);
  127. jedis.close();
  128. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  129. return true;
  130. }else{
  131. return false;
  132. }
  133. }
  134. /**
  135. * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
  136. * @param key
  137. * @param start
  138. * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li>
  139. * <li>如果start大于end,则返回一个空的列表,即列表被清空</li>
  140. * @param end
  141. * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li>
  142. * <li>可以超出索引,不影响结果</li>
  143. * @return boolean
  144. */
  145. public static boolean ltrim(String key, long start, long end){
  146. Jedis jedis = jedisPool.getResource();
  147. String statusCode = jedis.ltrim(key, start, end);
  148. jedis.close();
  149. if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  150. return true;
  151. }else{
  152. return false;
  153. }
  154. }
  155. /**
  156. * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null
  157. * @param key
  158. * @return String
  159. */
  160. public static String lpop(String key){
  161. Jedis jedis = jedisPool.getResource();
  162. String value = jedis.lpop(key);
  163. jedis.close();
  164. return value;
  165. }
  166. /**
  167. * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null
  168. * @param key
  169. * @return String
  170. */
  171. public static String rpop(String key){
  172. Jedis jedis = jedisPool.getResource();
  173. String value = jedis.rpop(key);
  174. jedis.close();
  175. return value;
  176. }
  177. /**
  178. * 在列表中的尾部添加一个个值,返回列表的长度
  179. * @param key
  180. * @param value
  181. * @return Long
  182. */
  183. public static Long rpush(String key, String value){
  184. Jedis jedis = jedisPool.getResource();
  185. Long length = jedis.rpush(key, value);
  186. jedis.close();
  187. return length;
  188. }
  189. /**
  190. * 在列表中的尾部添加多个值,返回列表的长度
  191. * @param key
  192. * @param values
  193. * @return Long
  194. */
  195. public static Long rpush(String key, String[] values){
  196. Jedis jedis = jedisPool.getResource();
  197. Long length = jedis.rpush(key, values);
  198. jedis.close();
  199. return length;
  200. }
  201. /**
  202. * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度
  203. * @param key
  204. * @param value
  205. * @return Long
  206. */
  207. public static Long rpushx(String key, String value){
  208. Jedis jedis = jedisPool.getResource();
  209. Long length = jedis.rpushx(key, value);
  210. jedis.close();
  211. return length;
  212. }
  213. /**
  214. * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
  215. * @param sourceKey 源列表的key,当源key不存在时,结果返回Null
  216. * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的
  217. * @return String
  218. */
  219. public static String rpopLpush(String sourceKey, String targetKey){
  220. Jedis jedis = jedisPool.getResource();
  221. String value = jedis.rpoplpush(sourceKey, targetKey);
  222. jedis.close();
  223. return value;
  224. }
  225. /**
  226. * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  227. * @param timeout 单位为秒
  228. * @param keys
  229. * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li>
  230. * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li>
  231. * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li>
  232. * @return List<String>
  233. */
  234. public static List<String> blpop(int timeout, String... keys){
  235. Jedis jedis = jedisPool.getResource();
  236. List<String> values = jedis.blpop(timeout, keys);
  237. jedis.close();
  238. return values;
  239. }
  240. /**
  241. * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  242. * @param timeout 单位为秒
  243. * @param keys
  244. * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li>
  245. * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li>
  246. * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li>
  247. * @return List<String>
  248. */
  249. public static List<String> brpop(int timeout, String... keys){
  250. Jedis jedis = jedisPool.getResource();
  251. List<String> values = jedis.brpop(timeout, keys);
  252. jedis.close();
  253. return values;
  254. }
  255. /**
  256. * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;
  257. * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  258. * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞
  259. * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的
  260. * @param timeout 单位为秒
  261. * @return String
  262. */
  263. public static String brpopLpush(String sourceKey, String targetKey, int timeout){
  264. Jedis jedis = jedisPool.getResource();
  265. String value = jedis.brpoplpush(sourceKey, targetKey, timeout);
  266. jedis.close();
  267. return value;
  268. }
  269. /**************************** redis 列表List end***************************/

Redis操作Set工具类封装,Java Redis Set命令封装

Java代码  
  1. /**************************** redis 集合Set start***************************/
  2. /**Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。**/
  3. /**
  4. * 向集合添加一个或多个成员,返回添加成功的数量
  5. * @param key
  6. * @param members
  7. * @return Long
  8. */
  9. public static Long sadd(String key, String... members){
  10. Jedis jedis = null;
  11. try {
  12. jedis = jedisPool.getResource();
  13. return jedis.sadd(key, members);
  14. }catch (Exception e) {
  15. e.printStackTrace();
  16. }finally{
  17. if(jedis != null){
  18. jedis.close();
  19. }
  20. }
  21. return null;
  22. }
  23. /**
  24. * 获取集合的成员数
  25. * @param key
  26. * @return
  27. */
  28. public static Long scard(String key){
  29. Jedis jedis = null;
  30. try {
  31. jedis = jedisPool.getResource();
  32. return jedis.scard(key);
  33. }catch (Exception e) {
  34. e.printStackTrace();
  35. }finally{
  36. if(jedis != null){
  37. jedis.close();
  38. }
  39. }
  40. return null;
  41. }
  42. /**
  43. * 返回集合中的所有成员
  44. * @param key
  45. * @return Set<String>
  46. */
  47. public static Set<String> smembers(String key){
  48. Jedis jedis = null;
  49. try {
  50. jedis = jedisPool.getResource();
  51. return jedis.smembers(key);
  52. }catch (Exception e) {
  53. e.printStackTrace();
  54. }finally{
  55. if(jedis != null){
  56. jedis.close();
  57. }
  58. }
  59. return null;
  60. }
  61. /**
  62. * 判断 member 元素是否是集合 key 的成员,在集合中返回True
  63. * @param key
  64. * @param member
  65. * @return Boolean
  66. */
  67. public static Boolean sIsMember(String key, String member){
  68. Jedis jedis = null;
  69. try {
  70. jedis = jedisPool.getResource();
  71. return jedis.sismember(key, member);
  72. }catch (Exception e) {
  73. e.printStackTrace();
  74. }finally{
  75. if(jedis != null){
  76. jedis.close();
  77. }
  78. }
  79. return null;
  80. }
  81. /**
  82. * 返回给定所有集合的差集(获取第一个key中与其它key不相同的值,当只有一个key时,就返回这个key的所有值)
  83. * @param keys
  84. * @return Set<String>
  85. */
  86. public static Set<String> sdiff(String... keys){
  87. Jedis jedis = null;
  88. try {
  89. jedis = jedisPool.getResource();
  90. return jedis.sdiff(keys);
  91. }catch (Exception e) {
  92. e.printStackTrace();
  93. }finally{
  94. if(jedis != null){
  95. jedis.close();
  96. }
  97. }
  98. return null;
  99. }
  100. /**
  101. * 返回给定所有集合的差集并存储在 targetKey中,类似sdiff,只是该方法把返回的差集保存到targetKey中
  102. * <li>当有差集时,返回true</li>
  103. * <li>当没有差集时,返回false</li>
  104. * @param targetKey
  105. * @param keys
  106. * @return
  107. */
  108. public static boolean sdiffStore(String targetKey, String... keys){
  109. Jedis jedis = null;
  110. try {
  111. jedis = jedisPool.getResource();
  112. Long statusCode = jedis.sdiffstore(targetKey, keys);
  113. if(SUCCESS_STATUS_LONG == statusCode){
  114. return true;
  115. }
  116. }catch (Exception e) {
  117. e.printStackTrace();
  118. }finally{
  119. if(jedis != null){
  120. jedis.close();
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * 返回给定所有集合的交集(获取第一个key中与其它key相同的值,要求所有key都要有相同的值,如果没有相同,返回Null。当只有一个key时,就返回这个key的所有值)
  127. * @param keys
  128. * @return Set<String>
  129. */
  130. public static Set<String> sinter(String... keys){
  131. Jedis jedis = null;
  132. try {
  133. jedis = jedisPool.getResource();
  134. return jedis.sinter(keys);
  135. }catch (Exception e) {
  136. e.printStackTrace();
  137. }finally{
  138. if(jedis != null){
  139. jedis.close();
  140. }
  141. }
  142. return null;
  143. }
  144. /**
  145. * 返回给定所有集合的交集并存储在 targetKey中,类似sinter
  146. * @param targetKey
  147. * @param keys
  148. * @return boolean
  149. */
  150. public static boolean sinterStore(String targetKey, String... keys){
  151. Jedis jedis = null;
  152. try {
  153. jedis = jedisPool.getResource();
  154. Long statusCode = jedis.sinterstore(targetKey, keys);
  155. if(SUCCESS_STATUS_LONG == statusCode){
  156. return true;
  157. }
  158. }catch (Exception e) {
  159. e.printStackTrace();
  160. }finally{
  161. if(jedis != null){
  162. jedis.close();
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * 将 member 元素从 sourceKey 集合移动到 targetKey 集合
  169. * <li>成功返回true</li>
  170. * <li>当member不存在于sourceKey时,返回false</li>
  171. * <li>当sourceKey不存在时,也返回false</li>
  172. * @param sourceKey
  173. * @param targetKey
  174. * @param member
  175. * @return boolean
  176. */
  177. public static boolean smove(String sourceKey, String targetKey, String member){
  178. Jedis jedis = null;
  179. try {
  180. jedis = jedisPool.getResource();
  181. Long value = jedis.smove(sourceKey, targetKey, member);
  182. if(value > 0){
  183. return true;
  184. }
  185. }catch (Exception e) {
  186. e.printStackTrace();
  187. }finally{
  188. if(jedis != null){
  189. jedis.close();
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * 移除并返回集合中的一个随机元素
  196. * <li>当set为空或者不存在时,返回Null</li>
  197. * @param key
  198. * @return String
  199. */
  200. public static String spop(String key){
  201. Jedis jedis = null;
  202. try {
  203. jedis = jedisPool.getResource();
  204. return jedis.spop(key);
  205. }catch (Exception e) {
  206. e.printStackTrace();
  207. }finally{
  208. if(jedis != null){
  209. jedis.close();
  210. }
  211. }
  212. return null;
  213. }
  214. /**
  215. * 返回集合中一个或多个随机数
  216. * <li>当count大于set的长度时,set所有值返回,不会抛错。</li>
  217. * <li>当count等于0时,返回[]</li>
  218. * <li>当count小于0时,也能返回。如-1返回一个,-2返回两个</li>
  219. * @param key
  220. * @param count
  221. * @return List<String>
  222. */
  223. public static List<String> srandMember(String key, int count){
  224. Jedis jedis = null;
  225. try {
  226. jedis = jedisPool.getResource();
  227. return jedis.srandmember(key, count);
  228. }catch (Exception e) {
  229. e.printStackTrace();
  230. }finally{
  231. if(jedis != null){
  232. jedis.close();
  233. }
  234. }
  235. return null;
  236. }
  237. /**
  238. * 移除集合中一个或多个成员
  239. * @param key
  240. * @param members
  241. * @return
  242. */
  243. public static boolean srem(String key, String... members){
  244. Jedis jedis = null;
  245. try {
  246. jedis = jedisPool.getResource();
  247. //Integer reply, specifically: 1 if the new element was removed
  248. //0 if the new element was not a member of the set
  249. Long value = jedis.srem(key, members);
  250. if(value > 0){
  251. return true;
  252. }
  253. }catch (Exception e) {
  254. e.printStackTrace();
  255. }finally{
  256. if(jedis != null){
  257. jedis.close();
  258. }
  259. }
  260. return false;
  261. }
  262. /**
  263. * 返回所有给定集合的并集,相同的只会返回一个
  264. * @param keys
  265. * @return
  266. */
  267. public static Set<String> sunion(String... keys){
  268. Jedis jedis = null;
  269. try {
  270. jedis = jedisPool.getResource();
  271. return jedis.sunion(keys);
  272. }catch (Exception e) {
  273. e.printStackTrace();
  274. }finally{
  275. if(jedis != null){
  276. jedis.close();
  277. }
  278. }
  279. return null;
  280. }
  281. /**
  282. * 所有给定集合的并集存储在targetKey集合中
  283. * <li>注:合并时,只会把keys中的集合返回,不包括targetKey本身</li>
  284. * <li>如果targetKey本身是有值的,合并后原来的值是没有的,因为把keys的集合重新赋值给targetKey</li>
  285. * <li>要想保留targetKey本身的值,keys要包含原来的targetKey</li>
  286. * @param targetKey
  287. * @param keys
  288. * @return
  289. */
  290. public static boolean sunionStore(String targetKey, String... keys){
  291. Jedis jedis = null;
  292. try {
  293. jedis = jedisPool.getResource();
  294. //返回合并后的长度
  295. Long statusCode = jedis.sunionstore(targetKey, keys);
  296. if(statusCode > 0){
  297. return true;
  298. }
  299. }catch (Exception e) {
  300. e.printStackTrace();
  301. }finally{
  302. if(jedis != null){
  303. jedis.close();
  304. }
  305. }
  306. return false;
  307. }
  308. /**************************** redis 集合Set end***************************/

IT忍者神龟之Redis操作工具类封装相关推荐

  1. Redis操作工具类

    前期文章中关于RedisOperateUtil工具类使用时需要强制类型转换,针对List等操作不方便,为此特意更新RedisOperateUtil工具类,前文链接:https://blog.csdn. ...

  2. SpringBoot Redis工具类封装

    SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable).看了很多博客后, ...

  3. Redis工具类封装RedisUtils(两种)

    RedisTemplate工具类1 本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需 ...

  4. Redis工具类封装讲解和实战

    Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download      ...

  5. Redis工具类封装

    Redis工具类封装 使用redis也好几年了,总是拷贝来拷贝去的,这次干脆放在这把,每次来这拷贝,不用在工程里面找来找去了. /*** Redis工具类* @author Huangliniao* ...

  6. redis-4.0.10集群安装(3台机器,6个node),以及在Spring项目中的集成,redis操作工具类

    1 Redis安装 redis高可用的三种常见的集群方式:redis sentinel .redis cluster(多主机+分布式).redis sharding.接下来主要介绍redis sent ...

  7. JAVA——文件操作工具类封装的简单实现

    问题描述 关于文件操作的简单工具类,包括文件夹创建,文件夹删除,文件创建,文件重命名,文件复制,文件删除.如果需要文件夹复制,其实就是创建文件夹和复制文件的操作. 解决方案 上下文 package c ...

  8. Redis工具类封装RedisUtils

    本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需要pom中加入对应依赖 <de ...

  9. RedisTemplate常用操作工具类封装,实现一个函数添加,删除,更新及对应批量操作

    使用示例 /*** @author evildoer* @since 2021-02-05*/ @Service @AllArgsConstructor public class QuestionSe ...

最新文章

  1. mysql reverse 索引_降序索引和减轻索引扫描
  2. wait和notify
  3. python2.7.13环境搭建
  4. Leetcode 415. 字符串相加 (每日一题 20210826 同类型题)
  5. 神策分析 1.16 版本上线场景库,实操方法论融入产品,全方位赋能多角色应用...
  6. 适配接口 java_【Java 设计模式】接口型模式--Adapter(适配器)模式
  7. 本地正常,部署放在IIS服务器上面偶尔会出现 列不属于表Table,因为多客户并发造成那个的问题
  8. uniapp ---- 树组件
  9. JavaNIO - Scatter Gather
  10. python curl 获取返回值_php curl_exec()函数 CURL获取返回值的方法
  11. python怎么排名次_2019:python第3次获得TIOBE最佳年度语言排名
  12. 软件架构设计-软件架构风格、分层架构
  13. vue 动态渲染表格序号列
  14. vs 2010旗舰版问题
  15. 虚拟机连接外网(桥接)
  16. html各种弹出框和提示框
  17. 计算机实验报告word结果总结,计算机网络实验报告.doc
  18. 植物大战 二叉树 概念——C
  19. 互联网暗潮汹涌,开放平台机遇空前
  20. 何海涛100题(1)自己心得

热门文章

  1. Fragment 嵌套ViewPager(ViewPager里面有多个Fragment)
  2. java中boolean转string_Java boolean转String
  3. STM32F4配置USART(DMA形式传输)
  4. C++中SIN的用法
  5. GaN的湿法化学蚀刻
  6. Safe Head机制技术理论分析
  7. vue左侧悬浮_Vue实现靠边悬浮球(PC端)
  8. PHP面向对象留言板(一)查询出JSON格式的留言板数据
  9. OpenCV转换HDR图像与源码分析
  10. GItLab入门级CI/CD环境搭建(适用于小微企业或个人)