1、配置文件

classpath路径下,新建redis.properties配置文件

配置文件内容

  1. # Redis settings
  2. redis.host=127.0.0.1
  3. redis.port=6379
  4. redis.timeout=10000
  5. redis.maxIdle=300
  6. redis.maxTotal=600
  7. # 毫秒
  8. redis.maxWaitMillis=1000
  9. redis.testOnBorrow=false

新建属性文件工具类,用来读取redis.properties配置文件

  1. /**
  2. * <p>属性文件工具类
  3. *
  4. * @author xupeng
  5. * @date 2019/10/28 10:39
  6. */
  7. public class PropertyUtil {
  8.  
  9. //加载property文件到io流里面
  10. public static Properties loadProperties(String fileName) {
  11. Properties properties = new Properties();
  12. try {
  13. InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName);
  14. properties.load(is);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. return properties;
  19. }
  20.  
  21. /**
  22. * 根据key值取得对应的value值
  23. *
  24. * @param key
  25. * @return
  26. */
  27. public static String getValue(String fileName, String key) {
  28. Properties properties = loadProperties(fileName);
  29. return properties.getProperty(key);
  30. }
  31.  
  32. }

新建Jedis工具类,封装常用方法

  1. /**
  2. * <p>Jedis工具类
  3. *
  4. * @author xupeng
  5. * @date 2019/10/28 11:02
  6. */
  7. public class JedisUtil {
  8.  
  9. private Logger logger = LoggerFactory.getLogger(JedisUtil.class);
  10.  
  11. private static JedisPool jedisPool = null;
  12.  
  13. private JedisUtil(){}
  14.  
  15. static {
  16. Properties properties = PropertyUtil.loadProperties("redis.properties");
  17. String host = properties.getProperty("redis.host");
  18. String port = properties.getProperty("redis.port");
  19. String timeout = properties.getProperty("redis.timeout");
  20. String maxIdle = properties.getProperty("redis.maxIdle");
  21. String maxTotal = properties.getProperty("redis.maxTotal");
  22. String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
  23. String testOnBorrow = properties.getProperty("redis.testOnBorrow");
  24.  
  25. JedisPoolConfig config = new JedisPoolConfig();
  26. config.setMaxIdle(Integer.parseInt(maxIdle));
  27. config.setMaxTotal(Integer.parseInt(maxTotal));
  28. config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
  29. config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));
  30. jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout));
  31. }
  32.  
  33. /**
  34. * <p>从jedis连接池中获取获取jedis对象
  35. */
  36. private Jedis getJedis() {
  37. return jedisPool.getResource();
  38. }
  39.  
  40. private static final JedisUtil jedisUtil = new JedisUtil();
  41.  
  42. /**
  43. * <p>获取JedisUtil实例
  44. */
  45. public static JedisUtil getInstance() {
  46. return jedisUtil;
  47. }
  48.  
  49. /**
  50. * <p>设置值
  51. * @param key 键
  52. * @param value 值
  53. */
  54. public String set(String key,String value){
  55. String status = null;
  56. Jedis jedis = null;
  57. try {
  58. jedis = getJedis();
  59. if (null != jedis){
  60. status = jedis.set(key, value);
  61. }
  62. }catch (Exception e){
  63. logger.info("Unable to get connection from connection pool");
  64. }finally {
  65. if (null != jedis){
  66. jedis.close();
  67. }
  68. }
  69. return status;
  70. }
  71.  
  72. /**
  73. * <p>根据键名称获取值
  74. * @param key 键名称
  75. * @return 值
  76. */
  77. public String get(String key){
  78. Jedis jedis = null;
  79. String value = null;
  80. try {
  81. jedis = getJedis();
  82. if (null != jedis){
  83. value = jedis.get(key);
  84.  
  85. }
  86. }catch (Exception e){
  87. logger.info("Unable to get connection from connection pool");
  88. }finally {
  89. if (null != jedis){
  90. jedis.close();
  91. }
  92. }
  93. return value;
  94. }
  95.  
  96. /**
  97. * <p>如果存在,不设置,返回0;不存在,进行设置,返回1。
  98. * @param key 键
  99. * @param value 值
  100. */
  101. public Long setnx(String key,String value){
  102. Long setnx = null;
  103. Jedis jedis = null;
  104. try {
  105. jedis = getJedis();
  106. if (null != jedis){
  107. setnx = jedis.setnx(key, value);
  108. }
  109. }catch (Exception e){
  110. logger.info("Unable to get connection from connection pool");
  111. }finally {
  112. if (null != jedis){
  113. jedis.close();
  114. }
  115. }
  116. return setnx;
  117. }
  118.  
  119. /**
  120. * <p>设置key的有效时间
  121. * @param key 键
  122. * @param value 值
  123. */
  124. public String setex(String key,int seconds,String value){
  125. String setex = null;
  126. Jedis jedis = null;
  127. try {
  128. jedis = getJedis();
  129. if (null != jedis){
  130. setex = jedis.setex(key, seconds, value);
  131. }
  132. }catch (Exception e){
  133. logger.info("Unable to get connection from connection pool");
  134. }finally {
  135. if (null != jedis){
  136. jedis.close();
  137. }
  138. }
  139. return setex;
  140. }
  141.  
  142. /**
  143. * <p>对某个值进行递增
  144. * @param key 键
  145. */
  146. public Long incr(String key){
  147. Long incr = null;
  148. Jedis jedis = null;
  149. try {
  150. jedis = getJedis();
  151. if (null != jedis){
  152. incr = jedis.incr(key);
  153. }
  154. }catch (Exception e){
  155. logger.info("Unable to get connection from connection pool");
  156. }finally {
  157. if (null != jedis){
  158. jedis.close();
  159. }
  160. }
  161. return incr;
  162. }
  163.  
  164. /**
  165. * <p>对某个值进行递减
  166. * @param key 键
  167. */
  168. public Long decr(String key){
  169. Long incr = null;
  170. Jedis jedis = null;
  171. try {
  172. jedis = getJedis();
  173. if (null != jedis){
  174. incr = jedis.decr(key);
  175. }
  176. }catch (Exception e){
  177. logger.info("Unable to get connection from connection pool");
  178. }finally {
  179. if (null != jedis){
  180. jedis.close();
  181. }
  182. }
  183. return incr;
  184. }
  185.  
  186. /**
  187. * <p>设置一个map类型的值
  188. *
  189. * @author zhuangxupeng
  190. * @date 2019/10/31 10:30
  191. */
  192. public String hmset(String key, Map<String,String> map){
  193. Jedis jedis = null;
  194. String hmset = null;
  195. try {
  196. jedis = getJedis();
  197. if (null != jedis){
  198. hmset = jedis.hmset(key, map);
  199. }
  200. }catch (Exception e){
  201. logger.info("Unable to get connection from connection pool");
  202. }finally {
  203. if (null != jedis){
  204. jedis.close();
  205. }
  206. }
  207. return hmset;
  208. }
  209.  
  210. /**
  211. * <p>获取一个map类型的值
  212. *
  213. * @author zhuangxupeng
  214. * @date 2019/10/31 10:30
  215. */
  216. public Map<String, String> hgetAll(String key){
  217. Jedis jedis = null;
  218. Map<String, String> map = null;
  219. try {
  220. jedis = getJedis();
  221. if (null != jedis){
  222. map = jedis.hgetAll(key);
  223. }
  224. }catch (Exception e){
  225. logger.info("Unable to get connection from connection pool");
  226. }finally {
  227. if (null != jedis){
  228. jedis.close();
  229. }
  230. }
  231. return map;
  232. }
  233.  
  234. /**
  235. * <p>获取key对应map的大小
  236. *
  237. * @author zhuangxupeng
  238. * @date 2019/10/31 10:30
  239. */
  240. public Long hlen(String key){
  241. Jedis jedis = null;
  242. Long hlen = null;
  243. try {
  244. jedis = getJedis();
  245. if (null != jedis){
  246. hlen = jedis.hlen(key);
  247. }
  248. }catch (Exception e){
  249. logger.info("Unable to get connection from connection pool");
  250. }finally {
  251. if (null != jedis){
  252. jedis.close();
  253. }
  254. }
  255. return hlen;
  256. }
  257.  
  258. /**
  259. * <p>获取key对应map的所有的键的集合
  260. *
  261. * @author zhuangxupeng
  262. * @date 2019/10/31 10:30
  263. */
  264. public Set<String> hkeys(String key){
  265. Jedis jedis = null;
  266. Set<String> sets = null;
  267. try {
  268. jedis = getJedis();
  269. if (null != jedis){
  270. sets = jedis.hkeys(key);
  271. }
  272. }catch (Exception e){
  273. logger.info("Unable to get connection from connection pool");
  274. }finally {
  275. if (null != jedis){
  276. jedis.close();
  277. }
  278. }
  279. return sets;
  280. }
  281.  
  282. /**
  283. * <p>获取key对应map的所有的值的集合
  284. *
  285. * @author zhuangxupeng
  286. * @date 2019/10/31 10:30
  287. */
  288. public List<String> hvals(String key){
  289. Jedis jedis = null;
  290. List<String> list = null;
  291. try {
  292. jedis = getJedis();
  293. if (null != jedis){
  294. list = jedis.hvals(key);
  295.  
  296. }
  297. }catch (Exception e){
  298. logger.info("Unable to get connection from connection pool");
  299. }finally {
  300. if (null != jedis){
  301. jedis.close();
  302. }
  303. }
  304. return list;
  305. }
  306.  
  307. /**
  308. * <p>删除
  309. *
  310. * @param key 键名称
  311. * @return del 删除成功返回1,失败返回0
  312. */
  313. public long del(String key) {
  314. Jedis jedis = null;
  315. Long del = null;
  316. try {
  317. jedis = getJedis();
  318. if (null != jedis){
  319. del = jedis.del(key);
  320. }
  321. }catch (Exception e){
  322. logger.info("Unable to get connection from connection pool");
  323. }finally {
  324. if (null != jedis){
  325. jedis.close();
  326. }
  327. }
  328. return del;
  329. }
  330.  
  331. /**
  332. * <p>是否存在KEY
  333. * @param key 键
  334. */
  335. public boolean exists(String key) {
  336. Jedis jedis = null;
  337. Boolean exists = null;
  338. try {
  339. jedis = getJedis();
  340. if (null != jedis){
  341. exists = jedis.exists(key);
  342. }
  343. }catch (Exception e){
  344. logger.info("Unable to get connection from connection pool");
  345. }finally {
  346. if (null != jedis){
  347. jedis.close();
  348. }
  349. }
  350. return exists;
  351. }
  352.  
  353. /**
  354. * <p>设置失效时间
  355. * @param key 键名称
  356. * @param seconds 秒数
  357. */
  358. public Long expire(String key, int seconds) {
  359. Jedis jedis = null;
  360. Long aLong = null;
  361. try {
  362. jedis = getJedis();
  363. if (null != jedis){
  364. aLong = jedis.expire(key, seconds);
  365. }
  366. }catch (Exception e){
  367. logger.info("Unable to get connection from connection pool");
  368. }finally {
  369. if (null != jedis){
  370. jedis.close();
  371. }
  372. }
  373. return aLong;
  374. }
  375.  
  376. /**
  377. * 删除失效时间
  378. * @param key
  379. */
  380. public Long persist(String key) {
  381. Jedis jedis = null;
  382. Long persist = null;
  383. try {
  384. jedis = getJedis();
  385. if (null != jedis){
  386. persist = jedis.persist(key);
  387. }
  388. }catch (Exception e){
  389. logger.info("Unable to get connection from connection pool");
  390. }finally {
  391. if (null != jedis){
  392. jedis.close();
  393. }
  394. }
  395. return persist;
  396. }
  397.  
  398. /**
  399. * <p>添加sorted set
  400. * <p>已经存在的值,再次添加会失败。
  401. *
  402. * @param key 键名称
  403. * @param score 分数值
  404. * @param value 实际值
  405. * @return zadd 成功返回1,失败返回0。
  406. */
  407. public Long zadd(String key,double score,String value) {
  408. Jedis jedis = null;
  409. Long zadd = null;
  410. try {
  411. jedis = getJedis();
  412. zadd = jedis.zadd(key, score, value);
  413. }catch (Exception e){
  414. logger.info("Unable to get connection from connection pool");
  415. }finally {
  416. if (null != jedis){
  417. jedis.close();
  418. }
  419. }
  420. return zadd;
  421. }
  422.  
  423. /**
  424. * 返回指定下标的集合元素
  425. * @param key
  426. * @param start 0为第一个
  427. * @param end -1为最后一个
  428. * @return
  429. */
  430. public Set<String> zrange(String key, int start, int end) {
  431. Jedis jedis = null;
  432. Set<String> sets = null;
  433. try {
  434. jedis = getJedis();
  435. sets = jedis.zrange(key, start, end);
  436. }catch (Exception e){
  437. logger.info("Unable to get connection from connection pool");
  438. }finally {
  439. if (null != jedis){
  440. jedis.close();
  441. }
  442. }
  443. return sets;
  444. }
  445.  
  446. }

写一个main方法,来进行简单测试

  1. /**
  2. * <p>Jedis客户端操作redis的string数据类型
  3. * @author xupeng
  4. * @date 2019年10月28日
  5. */
  6. public class JedisStringDemo {
  7. public static void main(String[] args) {
  8.  
  9. JedisUtil instance = JedisUtil.getInstance();
  10. instance.set("name", "zhuang");
  11.  
  12. String getNameVal = instance.get("name");
  13. System.out.println("get name value:" + getNameVal);
  14.  
  15. }
  16. }

Jedis API操作redis数据库的更多相关文章

  1. Redis学习(5)-Jedis(Java操作redis数据库技术)

    Java连接redis 一,导入jar包 Redis有什么命令,Jedis就有什么方法 设置防火墙 在Linux上面运行如下代码: 单实例:Jedis实例: package com.jedis.dem ...

  2. Jedis操作Redis数据库

    添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...

  3. Linux+Redis实战教程_day02_3、redis数据类型_4、String命令_5、hash命令_6、java操作redis数据库技术

    3. redis数据类型[重点] redis 使用的是键值对保存数据.(map) key:全部都是字符串 value:有五种数据类型 Key名:自定义,key名不要过长,否则影响使用效率 Key名不要 ...

  4. 转 用C API 操作MySQL数据库

    用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...

  5. 操作redis数据库 & 操作Excel & 开发接口

    操作redis数据库: string类型 1. 增 set,传俩个参数 key value(只要是字符串就行)2. 删 delete 传一个参数 key3. 修改 set 在目标key重新传参 key ...

  6. Python之操作redis数据库

    使用redis模块 一.操作redis 1.添加信息 (1)直接建key-value信息: 右键-Add New Key,手动添加key和value 右键-Console,打开控制台,写入命令 (2) ...

  7. redis python 操作 Python操作Redis数据库

    原文章于此:https://www.cnblogs.com/cnkai/p/7642787.html 有个人修改与改正 Python操作Redis数据库   连接数据库 StrictRedisfrom ...

  8. Jedis(java操作redis数据库技术)

    Redis有什么命令,Jedis就有什么方法. 客户端无法连接时,需要考虑防火墙配置,比如6379端口是否开放,也可以直接关闭防火墙. Jedis连接池: import org.junit.Test; ...

  9. 使用Jedis操作Redis数据库

    Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java的客户端,有Jedis.Redisson ...

随机推荐

  1. iview-admin本地测试上线登陆问题和文件路径找不到问题

    在项目中vue.config.js下修改上线路径(图中我修改为:根目录路径) 测试本地上线登陆出现问题: 在main.js下if (process.env.NODE_ENV !== 'producti ...

  2. js实现图片的Blob base64 ArrayBuffer 的各种转换

    一.相关基础知识 构造函数 FileReader() 返回一个新构造的FileReader 事件处理 FileReader.onabort  处理abort事件.该事件在读取操作被中断时触发. Fil ...

  3. VIO的一些随笔

    大公司跑在手机的似乎都是滤波MSCKF那种,有优化的但似乎功耗不行.还有就是杂交的前端滤波后面在挂地图,反正国内的似乎就是SVO, VINS, ORBSLAM,MSCKF组合起来. 缺啥补啥,那个太烂 ...

  4. deploy KBA 2167993

    The default trace shows the following error: ****************************************** Unable to cr ...

  5. IntelliJ IDEA.2017.3.4(win7 64位)的安装使用

    下载 1.Idea与Webstorm同为JetBrains公司的产品,因此安装使用方式也极为相似,首先我们打开IDEA的官方下载网站:https://www.jetbrains.com/idea/,点 ...

  6. mysql limit和offset用法

    limit和offset用法 mysql里分页一般用limit来实现 1. select* from article LIMIT 1,3 2.select * from article LIMIT 3 ...

  7. sql 四舍五入 保留两位小数

    一.问题描述 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 二.sqlserver解决方案: 1. 使用 Round() 函 ...

  8. Vue使用ref 属性来获取DOM

    注意,在父组件中可以使用this.$refs.属性名  获取任何元素的属性和方法,子组件不可以获取父组件中的 <!DOCTYPE html> <html lang="en& ...

  9. 再见 Docker,是时候拥抱下一代容器工具了

    本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. 「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章和 ...

  10. [Jenkins][centos]1 持续集成 之 配置VNC,部署Jenkins

    痛点:上一篇的AWS部署的VNC不知为啥挂了,死活连不上,因此改申请京东云做部署Jenkins 预计阅读时间:20分钟 更新软件,安装桌面 yum -y update yum -y groupinst ...