原文地址:http://blog.csdn.net/liuxiao723846/article/details/50401406

1、使用了jedis客户端,对Redis进行了封装,包括:

1)使用了redispool获取连接;以及连接的回收;

2)常用五种数据结构的常用操作封装;

[java] view plaincopy
  1. package redis.utils;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Set;
  5. //import org.apache.log4j.Logger;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisPool;
  8. import redis.clients.jedis.JedisPoolConfig;
  9. import redis.clients.jedis.SortingParams;
  10. import redis.clients.jedis.BinaryClient.LIST_POSITION;
  11. import redis.clients.util.SafeEncoder;
  12. /**
  13. * @author Mr.hu
  14. * @version crateTime:2013-10-30 下午5:41:30
  15. * Class Explain:JedisUtil
  16. */
  17. public class JedisUtil {
  18. //private Logger log = Logger.getLogger(this.getClass());
  19. /**缓存生存时间 */
  20. private final int expire = 60000;
  21. /** 操作Key的方法 */
  22. public Keys KEYS;
  23. /** 对存储结构为String类型的操作 */
  24. public Strings STRINGS;
  25. /** 对存储结构为List类型的操作 */
  26. public Lists LISTS;
  27. /** 对存储结构为Set类型的操作 */
  28. public Sets SETS;
  29. /** 对存储结构为HashMap类型的操作 */
  30. public Hash HASH;
  31. /** 对存储结构为Set(排序的)类型的操作 */
  32. public SortSet SORTSET;
  33. private static JedisPool jedisPool = null;
  34. private JedisUtil() {
  35. }
  36. static {
  37. JedisPoolConfig config = new JedisPoolConfig();
  38. //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
  39. //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
  40. config.setMaxTotal(500);
  41. //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
  42. config.setMaxIdle(5);
  43. //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
  44. config.setMaxWaitMillis(1000 * 100);
  45. //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
  46. config.setTestOnBorrow(true);
  47. //redis如果设置了密码:
  48. /*jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,
  49. JRedisPoolConfig.REDIS_PORT,
  50. 10000,JRedisPoolConfig.REDIS_PASSWORD);    */
  51. //redis未设置了密码:
  52. jedisPool = new JedisPool(config, "172.30.37.73",6379);
  53. }
  54. public JedisPool getPool() {
  55. return jedisPool;
  56. }
  57. /**
  58. * 从jedis连接池中获取获取jedis对象
  59. * @return
  60. */
  61. public Jedis getJedis() {
  62. return jedisPool.getResource();
  63. }
  64. private static final JedisUtil jedisUtil = new JedisUtil();
  65. /**
  66. * 获取JedisUtil实例
  67. * @return
  68. */
  69. public static JedisUtil getInstance() {
  70. return jedisUtil;
  71. }
  72. /**
  73. * 回收jedis(放到finally中)
  74. * @param jedis
  75. */
  76. public void returnJedis(Jedis jedis) {
  77. if (null != jedis && null != jedisPool) {
  78. jedisPool.returnResource(jedis);
  79. }
  80. }
  81. /**
  82. * 销毁连接(放到catch中)
  83. * @param pool
  84. * @param jedis
  85. */
  86. public static void returnBrokenResource(Jedis jedis) {
  87. if (null != jedis && null != jedisPool) {
  88. jedisPool.returnResource(jedis);
  89. }
  90. }
  91. /**
  92. * 设置过期时间
  93. * @author ruan 2013-4-11
  94. * @param key
  95. * @param seconds
  96. */
  97. public void expire(String key, int seconds) {
  98. if (seconds <= 0) {
  99. return;
  100. }
  101. Jedis jedis = getJedis();
  102. jedis.expire(key, seconds);
  103. returnJedis(jedis);
  104. }
  105. /**
  106. * 设置默认过期时间
  107. * @author ruan 2013-4-11
  108. * @param key
  109. */
  110. public void expire(String key) {
  111. expire(key, expire);
  112. }
  113. //*******************************************Keys*******************************************//
  114. public class Keys {
  115. /**
  116. * 清空所有key
  117. */
  118. public String flushAll() {
  119. Jedis jedis = getJedis();
  120. String stata = jedis.flushAll();
  121. returnJedis(jedis);
  122. return stata;
  123. }
  124. /**
  125. * 更改key
  126. * @param String oldkey
  127. * @param String  newkey
  128. * @return 状态码
  129. * */
  130. public String rename(String oldkey, String newkey) {
  131. return rename(SafeEncoder.encode(oldkey),
  132. SafeEncoder.encode(newkey));
  133. }
  134. /**
  135. * 更改key,仅当新key不存在时才执行
  136. * @param String oldkey
  137. * @param String newkey
  138. * @return 状态码
  139. * */
  140. public long renamenx(String oldkey, String newkey) {
  141. Jedis jedis = getJedis();
  142. long status = jedis.renamenx(oldkey, newkey);
  143. returnJedis(jedis);
  144. return status;
  145. }
  146. /**
  147. * 更改key
  148. * @param String oldkey
  149. * @param String newkey
  150. * @return 状态码
  151. * */
  152. public String rename(byte[] oldkey, byte[] newkey) {
  153. Jedis jedis = getJedis();
  154. String status = jedis.rename(oldkey, newkey);
  155. returnJedis(jedis);
  156. return status;
  157. }
  158. /**
  159. * 设置key的过期时间,以秒为单位
  160. * @param String key
  161. * @param 时间,已秒为单位
  162. * @return 影响的记录数
  163. * */
  164. public long expired(String key, int seconds) {
  165. Jedis jedis = getJedis();
  166. long count = jedis.expire(key, seconds);
  167. returnJedis(jedis);
  168. return count;
  169. }
  170. /**
  171. * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
  172. * @param String key
  173. * @param 时间,已秒为单位
  174. * @return 影响的记录数
  175. * */
  176. public long expireAt(String key, long timestamp) {
  177. Jedis jedis = getJedis();
  178. long count = jedis.expireAt(key, timestamp);
  179. returnJedis(jedis);
  180. return count;
  181. }
  182. /**
  183. * 查询key的过期时间
  184. * @param String key
  185. * @return 以秒为单位的时间表示
  186. * */
  187. public long ttl(String key) {
  188. //ShardedJedis sjedis = getShardedJedis();
  189. Jedis sjedis=getJedis();
  190. long len = sjedis.ttl(key);
  191. returnJedis(sjedis);
  192. return len;
  193. }
  194. /**
  195. * 取消对key过期时间的设置
  196. * @param key
  197. * @return 影响的记录数
  198. * */
  199. public long persist(String key) {
  200. Jedis jedis = getJedis();
  201. long count = jedis.persist(key);
  202. returnJedis(jedis);
  203. return count;
  204. }
  205. /**
  206. * 删除keys对应的记录,可以是多个key
  207. * @param String  ... keys
  208. * @return 删除的记录数
  209. * */
  210. public long del(String... keys) {
  211. Jedis jedis = getJedis();
  212. long count = jedis.del(keys);
  213. returnJedis(jedis);
  214. return count;
  215. }
  216. /**
  217. * 删除keys对应的记录,可以是多个key
  218. * @param String .. keys
  219. * @return 删除的记录数
  220. * */
  221. public long del(byte[]... keys) {
  222. Jedis jedis = getJedis();
  223. long count = jedis.del(keys);
  224. returnJedis(jedis);
  225. return count;
  226. }
  227. /**
  228. * 判断key是否存在
  229. * @param String key
  230. * @return boolean
  231. * */
  232. public boolean exists(String key) {
  233. //ShardedJedis sjedis = getShardedJedis();
  234. Jedis sjedis=getJedis();
  235. boolean exis = sjedis.exists(key);
  236. returnJedis(sjedis);
  237. return exis;
  238. }
  239. /**
  240. * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
  241. * @param String key
  242. * @return List<String> 集合的全部记录
  243. * **/
  244. public List<String> sort(String key) {
  245. //ShardedJedis sjedis = getShardedJedis();
  246. Jedis sjedis=getJedis();
  247. List<String> list = sjedis.sort(key);
  248. returnJedis(sjedis);
  249. return list;
  250. }
  251. /**
  252. * 对List,Set,SortSet进行排序或limit
  253. * @param String key
  254. * @param SortingParams parame 定义排序类型或limit的起止位置.
  255. * @return List<String> 全部或部分记录
  256. * **/
  257. public List<String> sort(String key, SortingParams parame) {
  258. //ShardedJedis sjedis = getShardedJedis();
  259. Jedis sjedis=getJedis();
  260. List<String> list = sjedis.sort(key, parame);
  261. returnJedis(sjedis);
  262. return list;
  263. }
  264. /**
  265. * 返回指定key存储的类型
  266. * @param String key
  267. * @return String string|list|set|zset|hash
  268. * **/
  269. public String type(String key) {
  270. //ShardedJedis sjedis = getShardedJedis();
  271. Jedis sjedis=getJedis();
  272. String type = sjedis.type(key);
  273. returnJedis(sjedis);
  274. return type;
  275. }
  276. /**
  277. * 查找所有匹配给定的模式的键
  278. * @param String  key的表达式,*表示多个,?表示一个
  279. * */
  280. public Set<String> keys(String pattern) {
  281. Jedis jedis = getJedis();
  282. Set<String> set = jedis.keys(pattern);
  283. returnJedis(jedis);
  284. return set;
  285. }
  286. }
  287. //*******************************************Sets*******************************************//
  288. public class Sets {
  289. /**
  290. * 向Set添加一条记录,如果member已存在返回0,否则返回1
  291. * @param String  key
  292. * @param String member
  293. * @return 操作码,0或1
  294. * */
  295. public long sadd(String key, String member) {
  296. Jedis jedis = getJedis();
  297. long s = jedis.sadd(key, member);
  298. returnJedis(jedis);
  299. return s;
  300. }
  301. public long sadd(byte[] key, byte[] member) {
  302. Jedis jedis = getJedis();
  303. long s = jedis.sadd(key, member);
  304. returnJedis(jedis);
  305. return s;
  306. }
  307. /**
  308. * 获取给定key中元素个数
  309. * @param String key
  310. * @return 元素个数
  311. * */
  312. public long scard(String key) {
  313. //ShardedJedis sjedis = getShardedJedis();
  314. Jedis sjedis = getJedis();
  315. long len = sjedis.scard(key);
  316. returnJedis(sjedis);
  317. return len;
  318. }
  319. /**
  320. * 返回从第一组和所有的给定集合之间的差异的成员
  321. * @param String ... keys
  322. * @return 差异的成员集合
  323. * */
  324. public Set<String> sdiff(String... keys) {
  325. Jedis jedis = getJedis();
  326. Set<String> set = jedis.sdiff(keys);
  327. returnJedis(jedis);
  328. return set;
  329. }
  330. /**
  331. * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
  332. * @param String newkey 新结果集的key
  333. * @param String ... keys 比较的集合
  334. * @return 新集合中的记录数
  335. * **/
  336. public long sdiffstore(String newkey, String... keys) {
  337. Jedis jedis = getJedis();
  338. long s = jedis.sdiffstore(newkey, keys);
  339. returnJedis(jedis);
  340. return s;
  341. }
  342. /**
  343. * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
  344. * @param String ... keys
  345. * @return 交集成员的集合
  346. * **/
  347. public Set<String> sinter(String... keys) {
  348. Jedis jedis = getJedis();
  349. Set<String> set = jedis.sinter(keys);
  350. returnJedis(jedis);
  351. return set;
  352. }
  353. /**
  354. * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
  355. * @param String  newkey 新结果集的key
  356. * @param String ... keys 比较的集合
  357. * @return 新集合中的记录数
  358. * **/
  359. public long sinterstore(String newkey, String... keys) {
  360. Jedis jedis = getJedis();
  361. long s = jedis.sinterstore(newkey, keys);
  362. returnJedis(jedis);
  363. return s;
  364. }
  365. /**
  366. * 确定一个给定的值是否存在
  367. * @param String  key
  368. * @param String member 要判断的值
  369. * @return 存在返回1,不存在返回0
  370. * **/
  371. public boolean sismember(String key, String member) {
  372. //ShardedJedis sjedis = getShardedJedis();
  373. Jedis sjedis = getJedis();
  374. boolean s = sjedis.sismember(key, member);
  375. returnJedis(sjedis);
  376. return s;
  377. }
  378. /**
  379. * 返回集合中的所有成员
  380. * @param String  key
  381. * @return 成员集合
  382. * */
  383. public Set<String> smembers(String key) {
  384. //ShardedJedis sjedis = getShardedJedis();
  385. Jedis sjedis = getJedis();
  386. Set<String> set = sjedis.smembers(key);
  387. returnJedis(sjedis);
  388. return set;
  389. }
  390. public Set<byte[]> smembers(byte[] key) {
  391. //ShardedJedis sjedis = getShardedJedis();
  392. Jedis sjedis = getJedis();
  393. Set<byte[]> set = sjedis.smembers(key);
  394. returnJedis(sjedis);
  395. return set;
  396. }
  397. /**
  398. * 将成员从源集合移出放入目标集合 <br/>
  399. * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
  400. * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
  401. * @param String  srckey 源集合
  402. * @param String dstkey 目标集合
  403. * @param String member 源集合中的成员
  404. * @return 状态码,1成功,0失败
  405. * */
  406. public long smove(String srckey, String dstkey, String member) {
  407. Jedis jedis = getJedis();
  408. long s = jedis.smove(srckey, dstkey, member);
  409. returnJedis(jedis);
  410. return s;
  411. }
  412. /**
  413. * 从集合中删除成员
  414. * @param String  key
  415. * @return 被删除的成员
  416. * */
  417. public String spop(String key) {
  418. Jedis jedis = getJedis();
  419. String s = jedis.spop(key);
  420. returnJedis(jedis);
  421. return s;
  422. }
  423. /**
  424. * 从集合中删除指定成员
  425. * @param String key
  426. * @param String  member 要删除的成员
  427. * @return 状态码,成功返回1,成员不存在返回0
  428. * */
  429. public long srem(String key, String member) {
  430. Jedis jedis = getJedis();
  431. long s = jedis.srem(key, member);
  432. returnJedis(jedis);
  433. return s;
  434. }
  435. /**
  436. * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
  437. * @param String  ... keys
  438. * @return 合并后的结果集合
  439. * @see sunionstore
  440. * */
  441. public Set<String> sunion(String... keys) {
  442. Jedis jedis = getJedis();
  443. Set<String> set = jedis.sunion(keys);
  444. returnJedis(jedis);
  445. return set;
  446. }
  447. /**
  448. * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
  449. * @param String  newkey 新集合的key
  450. * @param String ... keys 要合并的集合
  451. * **/
  452. public long sunionstore(String newkey, String... keys) {
  453. Jedis jedis = getJedis();
  454. long s = jedis.sunionstore(newkey, keys);
  455. returnJedis(jedis);
  456. return s;
  457. }
  458. }
  459. //*******************************************SortSet*******************************************//
  460. public class SortSet {
  461. /**
  462. * 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
  463. * @param String  key
  464. * @param double score 权重
  465. * @param String  member 要加入的值,
  466. * @return 状态码 1成功,0已存在member的值
  467. * */
  468. public long zadd(String key, double score, String member) {
  469. Jedis jedis = getJedis();
  470. long s = jedis.zadd(key, score, member);
  471. returnJedis(jedis);
  472. return s;
  473. }
  474. /*public long zadd(String key, Map<Double, String> scoreMembers) {
  475. Jedis jedis = getJedis();
  476. long s = jedis.zadd(key, scoreMembers);
  477. returnJedis(jedis);
  478. return s;
  479. }*/
  480. /**
  481. * 获取集合中元素的数量
  482. * @param String  key
  483. * @return 如果返回0则集合不存在
  484. * */
  485. public long zcard(String key) {
  486. //ShardedJedis sjedis = getShardedJedis();
  487. Jedis sjedis = getJedis();
  488. long len = sjedis.zcard(key);
  489. returnJedis(sjedis);
  490. return len;
  491. }
  492. /**
  493. * 获取指定权重区间内集合的数量
  494. * @param String key
  495. * @param double min 最小排序位置
  496. * @param double max 最大排序位置
  497. * */
  498. public long zcount(String key, double min, double max) {
  499. //ShardedJedis sjedis = getShardedJedis();
  500. Jedis sjedis = getJedis();
  501. long len = sjedis.zcount(key, min, max);
  502. returnJedis(sjedis);
  503. return len;
  504. }
  505. /**
  506. * 获得set的长度
  507. *
  508. * @param key
  509. * @return
  510. */
  511. public long zlength(String key) {
  512. long len = 0;
  513. Set<String> set = zrange(key, 0, -1);
  514. len = set.size();
  515. return len;
  516. }
  517. /**
  518. * 权重增加给定值,如果给定的member已存在
  519. * @param String  key
  520. * @param double score 要增的权重
  521. * @param String  member 要插入的值
  522. * @return 增后的权重
  523. * */
  524. public double zincrby(String key, double score, String member) {
  525. Jedis jedis = getJedis();
  526. double s = jedis.zincrby(key, score, member);
  527. returnJedis(jedis);
  528. return s;
  529. }
  530. /**
  531. * 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素
  532. * @param String key
  533. * @param int start 开始位置(包含)
  534. * @param int end 结束位置(包含)
  535. * @return Set<String>
  536. * */
  537. public Set<String> zrange(String key, int start, int end) {
  538. //ShardedJedis sjedis = getShardedJedis();
  539. Jedis sjedis = getJedis();
  540. Set<String> set = sjedis.zrange(key, start, end);
  541. returnJedis(sjedis);
  542. return set;
  543. }
  544. /**
  545. * 返回指定权重区间的元素集合
  546. * @param String key
  547. * @param double min 上限权重
  548. * @param double max 下限权重
  549. * @return Set<String>
  550. * */
  551. public Set<String> zrangeByScore(String key, double min, double max) {
  552. //ShardedJedis sjedis = getShardedJedis();
  553. Jedis sjedis = getJedis();
  554. Set<String> set = sjedis.zrangeByScore(key, min, max);
  555. returnJedis(sjedis);
  556. return set;
  557. }
  558. /**
  559. * 获取指定值在集合中的位置,集合排序从低到高
  560. * @see zrevrank
  561. * @param String key
  562. * @param String member
  563. * @return long 位置
  564. * */
  565. public long zrank(String key, String member) {
  566. //ShardedJedis sjedis = getShardedJedis();
  567. Jedis sjedis = getJedis();
  568. long index = sjedis.zrank(key, member);
  569. returnJedis(sjedis);
  570. return index;
  571. }
  572. /**
  573. * 获取指定值在集合中的位置,集合排序从高到低
  574. * @see zrank
  575. * @param String key
  576. * @param String member
  577. * @return long 位置
  578. * */
  579. public long zrevrank(String key, String member) {
  580. //ShardedJedis sjedis = getShardedJedis();
  581. Jedis sjedis = getJedis();
  582. long index = sjedis.zrevrank(key, member);
  583. returnJedis(sjedis);
  584. return index;
  585. }
  586. /**
  587. * 从集合中删除成员
  588. * @param String key
  589. * @param String member
  590. * @return 返回1成功
  591. * */
  592. public long zrem(String key, String member) {
  593. Jedis jedis = getJedis();
  594. long s = jedis.zrem(key, member);
  595. returnJedis(jedis);
  596. return s;
  597. }
  598. /**
  599. * 删除
  600. * @param key
  601. * @return
  602. */
  603. public long zrem(String key) {
  604. Jedis jedis = getJedis();
  605. long s = jedis.del(key);
  606. returnJedis(jedis);
  607. return s;
  608. }
  609. /**
  610. * 删除给定位置区间的元素
  611. * @param String  key
  612. * @param int start 开始区间,从0开始(包含)
  613. * @param int end 结束区间,-1为最后一个元素(包含)
  614. * @return 删除的数量
  615. * */
  616. public long zremrangeByRank(String key, int start, int end) {
  617. Jedis jedis = getJedis();
  618. long s = jedis.zremrangeByRank(key, start, end);
  619. returnJedis(jedis);
  620. return s;
  621. }
  622. /**
  623. * 删除给定权重区间的元素
  624. * @param String key
  625. * @param double min 下限权重(包含)
  626. * @param double max 上限权重(包含)
  627. * @return 删除的数量
  628. * */
  629. public long zremrangeByScore(String key, double min, double max) {
  630. Jedis jedis = getJedis();
  631. long s = jedis.zremrangeByScore(key, min, max);
  632. returnJedis(jedis);
  633. return s;
  634. }
  635. /**
  636. * 获取给定区间的元素,原始按照权重由高到低排序
  637. * @param String  key
  638. * @param int start
  639. * @param int end
  640. * @return Set<String>
  641. * */
  642. public Set<String> zrevrange(String key, int start, int end) {
  643. //ShardedJedis sjedis = getShardedJedis();
  644. Jedis sjedis = getJedis();
  645. Set<String> set = sjedis.zrevrange(key, start, end);
  646. returnJedis(sjedis);
  647. return set;
  648. }
  649. /**
  650. * 获取给定值在集合中的权重
  651. * @param String  key
  652. * @param memeber
  653. * @return double 权重
  654. * */
  655. public double zscore(String key, String memebr) {
  656. //ShardedJedis sjedis = getShardedJedis();
  657. Jedis sjedis = getJedis();
  658. Double score = sjedis.zscore(key, memebr);
  659. returnJedis(sjedis);
  660. if (score != null)
  661. return score;
  662. return 0;
  663. }
  664. }
  665. //*******************************************Hash*******************************************//
  666. public class Hash {
  667. /**
  668. * 从hash中删除指定的存储
  669. * @param String key
  670. * @param String  fieid 存储的名字
  671. * @return 状态码,1成功,0失败
  672. * */
  673. public long hdel(String key, String fieid) {
  674. Jedis jedis = getJedis();
  675. long s = jedis.hdel(key, fieid);
  676. returnJedis(jedis);
  677. return s;
  678. }
  679. public long hdel(String key) {
  680. Jedis jedis = getJedis();
  681. long s = jedis.del(key);
  682. returnJedis(jedis);
  683. return s;
  684. }
  685. /**
  686. * 测试hash中指定的存储是否存在
  687. * @param String key
  688. * @param String  fieid 存储的名字
  689. * @return 1存在,0不存在
  690. * */
  691. public boolean hexists(String key, String fieid) {
  692. //ShardedJedis sjedis = getShardedJedis();
  693. Jedis sjedis = getJedis();
  694. boolean s = sjedis.hexists(key, fieid);
  695. returnJedis(sjedis);
  696. return s;
  697. }
  698. /**
  699. * 返回hash中指定存储位置的值
  700. *
  701. * @param String key
  702. * @param String fieid 存储的名字
  703. * @return 存储对应的值
  704. * */
  705. public String hget(String key, String fieid) {
  706. //ShardedJedis sjedis = getShardedJedis();
  707. Jedis sjedis = getJedis();
  708. String s = sjedis.hget(key, fieid);
  709. returnJedis(sjedis);
  710. return s;
  711. }
  712. public byte[] hget(byte[] key, byte[] fieid) {
  713. //ShardedJedis sjedis = getShardedJedis();
  714. Jedis sjedis = getJedis();
  715. byte[] s = sjedis.hget(key, fieid);
  716. returnJedis(sjedis);
  717. return s;
  718. }
  719. /**
  720. * 以Map的形式返回hash中的存储和值
  721. * @param String    key
  722. * @return Map<Strinig,String>
  723. * */
  724. public Map<String, String> hgetAll(String key) {
  725. //ShardedJedis sjedis = getShardedJedis();
  726. Jedis sjedis = getJedis();
  727. Map<String, String> map = sjedis.hgetAll(key);
  728. returnJedis(sjedis);
  729. return map;
  730. }
  731. /**
  732. * 添加一个对应关系
  733. * @param String  key
  734. * @param String fieid
  735. * @param String value
  736. * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
  737. * **/
  738. public long hset(String key, String fieid, String value) {
  739. Jedis jedis = getJedis();
  740. long s = jedis.hset(key, fieid, value);
  741. returnJedis(jedis);
  742. return s;
  743. }
  744. public long hset(String key, String fieid, byte[] value) {
  745. Jedis jedis = getJedis();
  746. long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
  747. returnJedis(jedis);
  748. return s;
  749. }
  750. /**
  751. * 添加对应关系,只有在fieid不存在时才执行
  752. * @param String key
  753. * @param String fieid
  754. * @param String value
  755. * @return 状态码 1成功,0失败fieid已存
  756. * **/
  757. public long hsetnx(String key, String fieid, String value) {
  758. Jedis jedis = getJedis();
  759. long s = jedis.hsetnx(key, fieid, value);
  760. returnJedis(jedis);
  761. return s;
  762. }
  763. /**
  764. * 获取hash中value的集合
  765. *
  766. * @param String
  767. *            key
  768. * @return List<String>
  769. * */
  770. public List<String> hvals(String key) {
  771. //ShardedJedis sjedis = getShardedJedis();
  772. Jedis sjedis = getJedis();
  773. List<String> list = sjedis.hvals(key);
  774. returnJedis(sjedis);
  775. return list;
  776. }
  777. /**
  778. * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
  779. * @param String  key
  780. * @param String  fieid 存储位置
  781. * @param String long value 要增加的值,可以是负数
  782. * @return 增加指定数字后,存储位置的值
  783. * */
  784. public long hincrby(String key, String fieid, long value) {
  785. Jedis jedis = getJedis();
  786. long s = jedis.hincrBy(key, fieid, value);
  787. returnJedis(jedis);
  788. return s;
  789. }
  790. /**
  791. * 返回指定hash中的所有存储名字,类似Map中的keySet方法
  792. * @param String key
  793. * @return Set<String> 存储名称的集合
  794. * */
  795. public Set<String> hkeys(String key) {
  796. //ShardedJedis sjedis = getShardedJedis();
  797. Jedis sjedis = getJedis();
  798. Set<String> set = sjedis.hkeys(key);
  799. returnJedis(sjedis);
  800. return set;
  801. }
  802. /**
  803. * 获取hash中存储的个数,类似Map中size方法
  804. * @param String  key
  805. * @return long 存储的个数
  806. * */
  807. public long hlen(String key) {
  808. //ShardedJedis sjedis = getShardedJedis();
  809. Jedis sjedis = getJedis();
  810. long len = sjedis.hlen(key);
  811. returnJedis(sjedis);
  812. return len;
  813. }
  814. /**
  815. * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
  816. * @param String  key
  817. * @param String ... fieids 存储位置
  818. * @return List<String>
  819. * */
  820. public List<String> hmget(String key, String... fieids) {
  821. //ShardedJedis sjedis = getShardedJedis();
  822. Jedis sjedis = getJedis();
  823. List<String> list = sjedis.hmget(key, fieids);
  824. returnJedis(sjedis);
  825. return list;
  826. }
  827. public List<byte[]> hmget(byte[] key, byte[]... fieids) {
  828. //ShardedJedis sjedis = getShardedJedis();
  829. Jedis sjedis = getJedis();
  830. List<byte[]> list = sjedis.hmget(key, fieids);
  831. returnJedis(sjedis);
  832. return list;
  833. }
  834. /**
  835. * 添加对应关系,如果对应关系已存在,则覆盖
  836. * @param Strin   key
  837. * @param Map <String,String> 对应关系
  838. * @return 状态,成功返回OK
  839. * */
  840. public String hmset(String key, Map<String, String> map) {
  841. Jedis jedis = getJedis();
  842. String s = jedis.hmset(key, map);
  843. returnJedis(jedis);
  844. return s;
  845. }
  846. /**
  847. * 添加对应关系,如果对应关系已存在,则覆盖
  848. * @param Strin key
  849. * @param Map <String,String> 对应关系
  850. * @return 状态,成功返回OK
  851. * */
  852. public String hmset(byte[] key, Map<byte[], byte[]> map) {
  853. Jedis jedis = getJedis();
  854. String s = jedis.hmset(key, map);
  855. returnJedis(jedis);
  856. return s;
  857. }
  858. }
  859. //*******************************************Strings*******************************************//
  860. public class Strings {
  861. /**
  862. * 根据key获取记录
  863. * @param String  key
  864. * @return 值
  865. * */
  866. public String get(String key) {
  867. //ShardedJedis sjedis = getShardedJedis();
  868. Jedis sjedis = getJedis();
  869. String value = sjedis.get(key);
  870. returnJedis(sjedis);
  871. return value;
  872. }
  873. /**
  874. * 根据key获取记录
  875. * @param byte[] key
  876. * @return 值
  877. * */
  878. public byte[] get(byte[] key) {
  879. //ShardedJedis sjedis = getShardedJedis();
  880. Jedis sjedis = getJedis();
  881. byte[] value = sjedis.get(key);
  882. returnJedis(sjedis);
  883. return value;
  884. }
  885. /**
  886. * 添加有过期时间的记录
  887. *
  888. * @param String  key
  889. * @param int seconds 过期时间,以秒为单位
  890. * @param String value
  891. * @return String 操作状态
  892. * */
  893. public String setEx(String key, int seconds, String value) {
  894. Jedis jedis = getJedis();
  895. String str = jedis.setex(key, seconds, value);
  896. returnJedis(jedis);
  897. return str;
  898. }
  899. /**
  900. * 添加有过期时间的记录
  901. *
  902. * @param String key
  903. * @param int seconds 过期时间,以秒为单位
  904. * @param String  value
  905. * @return String 操作状态
  906. * */
  907. public String setEx(byte[] key, int seconds, byte[] value) {
  908. Jedis jedis = getJedis();
  909. String str = jedis.setex(key, seconds, value);
  910. returnJedis(jedis);
  911. return str;
  912. }
  913. /**
  914. * 添加一条记录,仅当给定的key不存在时才插入
  915. * @param String key
  916. * @param String value
  917. * @return long 状态码,1插入成功且key不存在,0未插入,key存在
  918. * */
  919. public long setnx(String key, String value) {
  920. Jedis jedis = getJedis();
  921. long str = jedis.setnx(key, value);
  922. returnJedis(jedis);
  923. return str;
  924. }
  925. /**
  926. * 添加记录,如果记录已存在将覆盖原有的value
  927. * @param String key
  928. * @param String value
  929. * @return 状态码
  930. * */
  931. public String set(String key, String value) {
  932. return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
  933. }
  934. /**
  935. * 添加记录,如果记录已存在将覆盖原有的value
  936. * @param String  key
  937. * @param String value
  938. * @return 状态码
  939. * */
  940. public String set(String key, byte[] value) {
  941. return set(SafeEncoder.encode(key), value);
  942. }
  943. /**
  944. * 添加记录,如果记录已存在将覆盖原有的value
  945. * @param byte[] key
  946. * @param byte[] value
  947. * @return 状态码
  948. * */
  949. public String set(byte[] key, byte[] value) {
  950. Jedis jedis = getJedis();
  951. String status = jedis.set(key, value);
  952. returnJedis(jedis);
  953. return status;
  954. }
  955. /**
  956. * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
  957. * 例:String str1="123456789";<br/>
  958. * 对str1操作后setRange(key,4,0000),str1="123400009";
  959. * @param String  key
  960. * @param long offset
  961. * @param String  value
  962. * @return long value的长度
  963. * */
  964. public long setRange(String key, long offset, String value) {
  965. Jedis jedis = getJedis();
  966. long len = jedis.setrange(key, offset, value);
  967. returnJedis(jedis);
  968. return len;
  969. }
  970. /**
  971. * 在指定的key中追加value
  972. * @param String  key
  973. * @param String value
  974. * @return long 追加后value的长度
  975. * **/
  976. public long append(String key, String value) {
  977. Jedis jedis = getJedis();
  978. long len = jedis.append(key, value);
  979. returnJedis(jedis);
  980. return len;
  981. }
  982. /**
  983. * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
  984. * @param String key
  985. * @param long number 要减去的值
  986. * @return long 减指定值后的值
  987. * */
  988. public long decrBy(String key, long number) {
  989. Jedis jedis = getJedis();
  990. long len = jedis.decrBy(key, number);
  991. returnJedis(jedis);
  992. return len;
  993. }
  994. /**
  995. * <b>可以作为获取唯一id的方法</b><br/>
  996. * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
  997. * @param String  key
  998. * @param long number 要减去的值
  999. * @return long 相加后的值
  1000. * */
  1001. public long incrBy(String key, long number) {
  1002. Jedis jedis = getJedis();
  1003. long len = jedis.incrBy(key, number);
  1004. returnJedis(jedis);
  1005. return len;
  1006. }
  1007. /**
  1008. * 对指定key对应的value进行截取
  1009. * @param String   key
  1010. * @param long startOffset 开始位置(包含)
  1011. * @param long endOffset 结束位置(包含)
  1012. * @return String 截取的值
  1013. * */
  1014. public String getrange(String key, long startOffset, long endOffset) {
  1015. //ShardedJedis sjedis = getShardedJedis();
  1016. Jedis sjedis = getJedis();
  1017. String value = sjedis.getrange(key, startOffset, endOffset);
  1018. returnJedis(sjedis);
  1019. return value;
  1020. }
  1021. /**
  1022. * 获取并设置指定key对应的value<br/>
  1023. * 如果key存在返回之前的value,否则返回null
  1024. * @param String  key
  1025. * @param String value
  1026. * @return String 原始value或null
  1027. * */
  1028. public String getSet(String key, String value) {
  1029. Jedis jedis = getJedis();
  1030. String str = jedis.getSet(key, value);
  1031. returnJedis(jedis);
  1032. return str;
  1033. }
  1034. /**
  1035. * 批量获取记录,如果指定的key不存在返回List的对应位置将是null
  1036. * @param String keys
  1037. * @return List<String> 值得集合
  1038. * */
  1039. public List<String> mget(String... keys) {
  1040. Jedis jedis = getJedis();
  1041. List<String> str = jedis.mget(keys);
  1042. returnJedis(jedis);
  1043. return str;
  1044. }
  1045. /**
  1046. * 批量存储记录
  1047. * @param String keysvalues 例:keysvalues="key1","value1","key2","value2";
  1048. * @return String 状态码
  1049. * */
  1050. public String mset(String... keysvalues) {
  1051. Jedis jedis = getJedis();
  1052. String str = jedis.mset(keysvalues);
  1053. returnJedis(jedis);
  1054. return str;
  1055. }
  1056. /**
  1057. * 获取key对应的值的长度
  1058. * @param String key
  1059. * @return value值得长度
  1060. * */
  1061. public long strlen(String key) {
  1062. Jedis jedis = getJedis();
  1063. long len = jedis.strlen(key);
  1064. returnJedis(jedis);
  1065. return len;
  1066. }
  1067. }
  1068. //*******************************************Lists*******************************************//
  1069. public class Lists {
  1070. /**
  1071. * List长度
  1072. * @param String key
  1073. * @return 长度
  1074. * */
  1075. public long llen(String key) {
  1076. return llen(SafeEncoder.encode(key));
  1077. }
  1078. /**
  1079. * List长度
  1080. * @param byte[] key
  1081. * @return 长度
  1082. * */
  1083. public long llen(byte[] key) {
  1084. //ShardedJedis sjedis = getShardedJedis();
  1085. Jedis sjedis = getJedis();
  1086. long count = sjedis.llen(key);
  1087. returnJedis(sjedis);
  1088. return count;
  1089. }
  1090. /**
  1091. * 覆盖操作,将覆盖List中指定位置的值
  1092. * @param byte[] key
  1093. * @param int index 位置
  1094. * @param byte[] value 值
  1095. * @return 状态码
  1096. * */
  1097. public String lset(byte[] key, int index, byte[] value) {
  1098. Jedis jedis = getJedis();
  1099. String status = jedis.lset(key, index, value);
  1100. returnJedis(jedis);
  1101. return status;
  1102. }
  1103. /**
  1104. * 覆盖操作,将覆盖List中指定位置的值
  1105. * @param key
  1106. * @param int index 位置
  1107. * @param String  value 值
  1108. * @return 状态码
  1109. * */
  1110. public String lset(String key, int index, String value) {
  1111. return lset(SafeEncoder.encode(key), index,
  1112. SafeEncoder.encode(value));
  1113. }
  1114. /**
  1115. * 在value的相对位置插入记录
  1116. * @param key
  1117. * @param LIST_POSITION   前面插入或后面插入
  1118. * @param String pivot 相对位置的内容
  1119. * @param String value 插入的内容
  1120. * @return 记录总数
  1121. * */
  1122. public long linsert(String key, LIST_POSITION where, String pivot,
  1123. String value) {
  1124. return linsert(SafeEncoder.encode(key), where,
  1125. SafeEncoder.encode(pivot), SafeEncoder.encode(value));
  1126. }
  1127. /**
  1128. * 在指定位置插入记录
  1129. * @param String key
  1130. * @param LIST_POSITION 前面插入或后面插入
  1131. * @param byte[] pivot 相对位置的内容
  1132. * @param byte[] value 插入的内容
  1133. * @return 记录总数
  1134. * */
  1135. public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,
  1136. byte[] value) {
  1137. Jedis jedis = getJedis();
  1138. long count = jedis.linsert(key, where, pivot, value);
  1139. returnJedis(jedis);
  1140. return count;
  1141. }
  1142. /**
  1143. * 获取List中指定位置的值
  1144. * @param String  key
  1145. * @param int index 位置
  1146. * @return 值
  1147. * **/
  1148. public String lindex(String key, int index) {
  1149. return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
  1150. }
  1151. /**
  1152. * 获取List中指定位置的值
  1153. * @param byte[] key
  1154. * @param int index 位置
  1155. * @return 值
  1156. * **/
  1157. public byte[] lindex(byte[] key, int index) {
  1158. //ShardedJedis sjedis = getShardedJedis();
  1159. Jedis sjedis = getJedis();
  1160. byte[] value = sjedis.lindex(key, index);
  1161. returnJedis(sjedis);
  1162. return value;
  1163. }
  1164. /**
  1165. * 将List中的第一条记录移出List
  1166. * @param String key
  1167. * @return 移出的记录
  1168. * */
  1169. public String lpop(String key) {
  1170. return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
  1171. }
  1172. /**
  1173. * 将List中的第一条记录移出List
  1174. * @param byte[] key
  1175. * @return 移出的记录
  1176. * */
  1177. public byte[] lpop(byte[] key) {
  1178. Jedis jedis = getJedis();
  1179. byte[] value = jedis.lpop(key);
  1180. returnJedis(jedis);
  1181. return value;
  1182. }
  1183. /**
  1184. * 将List中最后第一条记录移出List
  1185. *
  1186. * @param byte[] key
  1187. * @return 移出的记录
  1188. * */
  1189. public String rpop(String key) {
  1190. Jedis jedis = getJedis();
  1191. String value = jedis.rpop(key);
  1192. returnJedis(jedis);
  1193. return value;
  1194. }
  1195. /**
  1196. * 向List尾部追加记录
  1197. * @param String key
  1198. * @param String value
  1199. * @return 记录总数
  1200. * */
  1201. public long lpush(String key, String value) {
  1202. return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
  1203. }
  1204. /**
  1205. * 向List头部追加记录
  1206. * @param String  key
  1207. * @param String  value
  1208. * @return 记录总数
  1209. * */
  1210. public long rpush(String key, String value) {
  1211. Jedis jedis = getJedis();
  1212. long count = jedis.rpush(key, value);
  1213. returnJedis(jedis);
  1214. return count;
  1215. }
  1216. /**
  1217. * 向List头部追加记录
  1218. * @param String key
  1219. * @param String value
  1220. * @return 记录总数
  1221. * */
  1222. public long rpush(byte[] key, byte[] value) {
  1223. Jedis jedis = getJedis();
  1224. long count = jedis.rpush(key, value);
  1225. returnJedis(jedis);
  1226. return count;
  1227. }
  1228. /**
  1229. * 向List中追加记录
  1230. * @param byte[] key
  1231. * @param byte[] value
  1232. * @return 记录总数
  1233. * */
  1234. public long lpush(byte[] key, byte[] value) {
  1235. Jedis jedis = getJedis();
  1236. long count = jedis.lpush(key, value);
  1237. returnJedis(jedis);
  1238. return count;
  1239. }
  1240. /**
  1241. * 获取指定范围的记录,可以做为分页使用
  1242. * @param String key
  1243. * @param long start
  1244. * @param long end
  1245. * @return List
  1246. * */
  1247. public List<String> lrange(String key, long start, long end) {
  1248. //ShardedJedis sjedis = getShardedJedis();
  1249. Jedis sjedis = getJedis();
  1250. List<String> list = sjedis.lrange(key, start, end);
  1251. returnJedis(sjedis);
  1252. return list;
  1253. }
  1254. /**
  1255. * 获取指定范围的记录,可以做为分页使用
  1256. * @param byte[] key
  1257. * @param int start
  1258. * @param int end 如果为负数,则尾部开始计算
  1259. * @return List
  1260. * */
  1261. public List<byte[]> lrange(byte[] key, int start, int end) {
  1262. //ShardedJedis sjedis = getShardedJedis();
  1263. Jedis sjedis = getJedis();
  1264. List<byte[]> list = sjedis.lrange(key, start, end);
  1265. returnJedis(sjedis);
  1266. return list;
  1267. }
  1268. /**
  1269. * 删除List中c条记录,被删除的记录值为value
  1270. * @param byte[] key
  1271. * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
  1272. * @param byte[] value 要匹配的值
  1273. * @return 删除后的List中的记录数
  1274. * */
  1275. public long lrem(byte[] key, int c, byte[] value) {
  1276. Jedis jedis = getJedis();
  1277. long count = jedis.lrem(key, c, value);
  1278. returnJedis(jedis);
  1279. return count;
  1280. }
  1281. /**
  1282. * 删除List中c条记录,被删除的记录值为value
  1283. * @param String key
  1284. * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
  1285. * @param String value 要匹配的值
  1286. * @return 删除后的List中的记录数
  1287. * */
  1288. public long lrem(String key, int c, String value) {
  1289. return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
  1290. }
  1291. /**
  1292. * 算是删除吧,只保留start与end之间的记录
  1293. * @param byte[] key
  1294. * @param int start 记录的开始位置(0表示第一条记录)
  1295. * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
  1296. * @return 执行状态码
  1297. * */
  1298. public String ltrim(byte[] key, int start, int end) {
  1299. Jedis jedis = getJedis();
  1300. String str = jedis.ltrim(key, start, end);
  1301. returnJedis(jedis);
  1302. return str;
  1303. }
  1304. /**
  1305. * 算是删除吧,只保留start与end之间的记录
  1306. * @param String key
  1307. * @param int start 记录的开始位置(0表示第一条记录)
  1308. * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
  1309. * @return 执行状态码
  1310. * */
  1311. public String ltrim(String key, int start, int end) {
  1312. return ltrim(SafeEncoder.encode(key), start, end);
  1313. }
  1314. }
  1315. public static void main(String[] args) {
  1316. JedisUtil jedisUtil= JedisUtil.getInstance();
  1317. JedisUtil.Strings strings=jedisUtil.new Strings();
  1318. strings.set("nnn", "nnnn");
  1319. System.out.println("-----"+strings.get("nnn"));
  1320. Jedis jedis=JedisUtil.getInstance().getJedis();
  1321. for (int i = 0; i < 10; i++) {
  1322. jedis.set("test", "test");
  1323. System.out.println(i+"=="+jedis.get("test"));
  1324. }
  1325. JedisUtil.getInstance().returnJedis(jedis);
  1326. }
  1327. }

2、序列化、反序列化:

redis服务器本身支持二进制安全的类型,所以可以把一个Java对象序列化后存储到redis中。下面封装了一个序列化、反序列化的工具类:

[java] view plaincopy
  1. package redis.utils;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. public class SerializeUtil {
  7. /**
  8. * 序列化
  9. *
  10. * @param object
  11. * @return
  12. */
  13. public static byte[] serialize(Object object) {
  14. ObjectOutputStream oos = null;
  15. ByteArrayOutputStream baos = null;
  16. try {
  17. // 序列化
  18. baos = new ByteArrayOutputStream();
  19. oos = new ObjectOutputStream(baos);
  20. oos.writeObject(object);
  21. byte[] bytes = baos.toByteArray();
  22. return bytes;
  23. } catch (Exception e) {
  24. }
  25. return null;
  26. }
  27. /**
  28. * 反序列化
  29. *
  30. * @param bytes
  31. * @return
  32. */
  33. public static Object unserialize(byte[] bytes) {
  34. ByteArrayInputStream bais = null;
  35. try {
  36. // 反序列化
  37. bais = new ByteArrayInputStream(bytes);
  38. ObjectInputStream ois = new ObjectInputStream(bais);
  39. return ois.readObject();
  40. } catch (Exception e) {
  41. }
  42. return null;
  43. }
  44. }

3、测试:

1)直接使用RedisUtils实例进行五大数据类型的操作:(这样,使用完后会自动归还到池子中)

[html] view plaincopy
  1. JedisUtil jedisUtil= JedisUtil.getInstance();
  2. JedisUtil.Strings strings=jedisUtil.new Strings();
  3. strings.set("nnn", "nnnn");
  4. System.out.println("-----"+strings.get("nnn"));

2)通过RedisUtil实例获取Jedis连接对象;这样就可以用原生的方式使用;最后使用完后需要手动将其归还到池子中:

[java] view plaincopy
  1. Jedis jedis=JedisUtil.getInstance().getJedis();
  2. for (int i = 0; i < 10; i++) {
  3. jedis.set("test", "test");
  4. System.out.println(i+"=="+jedis.get("test"));
  5. }
  6. JedisUtil.getInstance().returnJedis(jedis);

3)将java对象存到redis中:

[java] view plaincopy
  1. Person p = new Person();
  2. p.setId(3);
  3. p.setName("测试");
  4. JedisUtil.Strings strings=jedisUtil.new Strings();
  5. strings.set("object3", SerializeUtil.serialize(p));
  6. //jedis.set(SafeEncoder.encode("object1"),SerializeUtil.serialize(p));
  7. byte[] personBy = jedis.get(SafeEncoder.encode("object3"));
  8. Person p1 = (Person) SerializeUtil.unserialize(personBy);
  9. System.out.println(p1.getName());

转载于:https://www.cnblogs.com/austinspark-jessylu/p/7357805.html

RedisUtil工具类相关推荐

  1. Redis学习篇3_事务及其监控(锁)、Jedis、SpringBoot整合Redis、RedisTemplate的json序列化、RedisUtil工具类

    目录 事务及其监控(锁) Jedis SpringBoot整合Redis RedisTemplate 默认RedisTemplate来源 关于中文序列化问题 RedisUtil工具类 一.事务及其监控 ...

  2. RedisUtil,Redis工具类

    RedisUtil,Redis工具类 1.配置maven,增加依赖 2.配置工具类 1.配置maven,增加依赖 <dependency><groupId>redis.clie ...

  3. Redis的API调用工具类

    1.一个Redis的工具类!方便调用Redis的API操作! package com.example.demo.utils;import org.springframework.beans.facto ...

  4. Redis使用及工具类

    原地址:https://www.cnblogs.com/wyy123/p/6078593.html [学会安装redis] 从redis.io下载最新版redis-X.Y.Z.tar.gz后解压,然后 ...

  5. springboot使用redisTemplate 报错:APP FAILED TO START Field template in required a single bean redis工具类

    springboot使用redisTemplate 报错: template in com.j.ssm.tool.RedisUtil required a single bean, but 2 wer ...

  6. java redis remove_最全的Java操作Redis的工具类

    RedisUtil 当前版本:1.1 增加更全的方法,对以前的部分方法进行了规范命名,请放心替换成新版本. 介绍 最全的Java操作Redis的工具类,使用StringRedisTemplate实现, ...

  7. 封装自定义的redis切库工具类ByteArrayRedisTemplate,读取byte数组反序列化成List<Object>

    封装自定义的redis切库工具类ByteArrayRedisTemplate,读取byte数组反序列化成List<Object>(使用lettuce连接池) 代码环境 框架:springb ...

  8. 【redis系列】redisTemplate缓存常用工具类

    背景 日常开发过程中,大家使用redis缓存基本上是家常便饭,但是代码中使用redisTemplate组件会略显得麻烦,使用时需要开发人员查阅官网文档,具体场景使用哪些方法,会花费相对的时间,故小编为 ...

  9. 基于Redis GEO(地理位置) 实现附近的人,商家等相关功能实现 使用SpringBoot Redis工具类

    Redis GEO 1.基本介绍 1.Redis GEO 2.基础语法 GEOADD GEOPOS GEODIST GEORADIUS GEOHASH 2.可用于实现的功能 3.SpringBoot实 ...

最新文章

  1. python正确方法,方法 - 廖雪峰的官方网站
  2. 警告: Could not load driverClass com.mysql.cj.jdbc.Driver
  3. es java_JAVA API操作ES详解
  4. 嵌入式linux如何下载程序,Linux平台的下载程序-嵌入式系统-与非网
  5. c语言中宏替换时的顺序
  6. 字符串里解析vue表达式
  7. 进入磁盘指令_Linux操作指令整理
  8. javaScript的常见document对象
  9. 罗永浩回应被列老赖;三星解散自研 CPU 团队;Python 采用 12 个月的发布周期 | 极客头条...
  10. 【Uva 11280 飞到弗雷德里顿】
  11. python机器学习库xgboost——xgboost算法
  12. 教你在Linux操作系统中如何创建函数库
  13. ubuntu下C语言打开bmp图像文件并读取数据
  14. php 调用高拍仪,html页面通过ActiveX控件调用摄像头实现拍照上传demo代码下载
  15. 模型训练测试之三:yolov5 模型训练及windows部署(一)
  16. oracle财务系统表,Oracle ERP 财务模块表结构.ppt
  17. java编程规范换行_Java源代码的换行规则
  18. 【ORB_SLAM3源码解读】IMU基础介绍、IMU姿态、速度、位置解算以及误差方程、坐标系
  19. 微信图片,此图片来自微信公众平台未经允许不可引用解决方案
  20. 清华姚班陈丹琦等27位华人学者获奖,斯隆奖2022年获奖名单颁布

热门文章

  1. CentOS 7 搭建 Ceph 集群(nautilus 版本)
  2. java将数组置零的函数,Java Script 数组内置函数
  3. 20春计算机应用基础在线作业,19春学期《计算机应用基础》在线作业21.txt
  4. 开发商微信选房后不退认筹金_新楼盘开盘的“认筹”和“认购”,劝您看懂后再去认!...
  5. linux 会不会受到永恒之蓝漏洞,永恒之蓝漏洞复现(ms17-010)
  6. java浮点运算很难_关于Java:浮点运算不能产生精确结果
  7. Coprime Sequence
  8. 数据结构—线索二叉树
  9. 数据结构—快速排序及其实现思想分而治之DC(思维导图版)
  10. 牛客网 二叉搜索树与双向链表