windows下redis的配置和jedis api的最基本的使用
redis的安装直接跳过
1.注册redis服务
在DOM窗口下,进入redis的安装目录(可以先进入安装目录,然后shift+右键,选择在此处打开powershell窗口),
输入命令:
redis-server --service-install redis.windows-service.conf --loglevel verbose;
开启redis服务
打开DOM窗口,输入命令:
redis-server --service-start
关闭redis服务
打开DOM窗口,输入命令:
redis-server --service-stop
到此,redis在windows下的配置便告完成。
2.jedis api的使用
maven引入jedis的依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
redis.properties文件定义了jedis连接池的一些参数和redis服务器的信息
#config for redis
#最大连接数
redis.pool.maxActive=512
#最大空闲连接数
redis.pool.maxIdle=100
#最大等待等待时间
redis.pool.maxWait=100000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=127.0.0.1
redis.port=6379
#设置过期时间为2小时,超过过期时间会自动删除key-value
redis.expire=7200
注意:在实际的项目文件中不允许出现中文注解
RedisProvider类定义如何获得与redis的连接和释放与redis的连接
public class RedisProvider {
//protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class);
protected static JedisPool jedispool;
protected static int EXPIRE = 130;
static{
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null) {
throw new IllegalArgumentException(
"[redis.properties] is not found!");
}
EXPIRE = Integer.valueOf(bundle.getString("redis.expire"));
JedisPoolConfig jedisconfig = new JedisPoolConfig();
jedisconfig.setMaxActive(Integer.valueOf(bundle
.getString("redis.pool.maxActive")));
jedisconfig.setMaxIdle(Integer.valueOf(bundle
.getString("redis.pool.maxIdle")));
jedisconfig.setMaxWait(Long.valueOf(bundle
.getString("redis.pool.maxWait")));
jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle
.getString("redis.pool.testOnBorrow")));
jedisconfig.setTestOnReturn(Boolean.valueOf(bundle
.getString("redis.pool.testOnReturn")));
jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),
Integer.valueOf(bundle.getString("redis.port")), 100000);
}
/*获取资源*/
public static Jedis getJedis() {
Jedis jedis = null;
try {
jedis = jedispool.getResource();
} catch (JedisConnectionException jce) {
ExceptionUtil.getTrace(jce);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
ExceptionUtil.getTrace(e);
}
jedis = jedispool.getResource();
}
return jedis;
}
/*释放资源*/
public static void returnResource(JedisPool pool, Jedis jedis) {
if (jedis != null) {
pool.returnResource(jedis);
}
}
}
/*资源jedis理解为连接。jedispool理解为连接池*/
RedisHelper定义了利用jedis连接堆redis服务器端进行的各种操作,这里出于应用的需要,只实现了基本的set和get操作,更多的jedis操作可见它的api:
https://tool.oschina.net/uploads/apidocs/
public class RedisHelper extends RedisProvider {
/**
* Set the string value as value of the key. Default settings at save
* time(2000s)
*
* @param key
* @param value
* @return
*/
public static String set(String key, String value) {
Jedis jedis = null;
String rtn = null;
try {
jedis = getJedis();
rtn = jedis.setex(key, EXPIRE, value);
} catch (Exception e) {
//LOG.error(ExceptionUtil.getTrace(e));
jedispool.returnBrokenResource(jedis);
} finally {
returnResource(jedispool, jedis);
}
return rtn;
}
public static String get(String key) {
Jedis jedis = null;
String rtn = null;
try {
jedis = getJedis();
rtn = jedis.get(key);
} catch (Exception e) {
//LOG.error(ExceptionUtil.getTrace(e));
jedispool.returnBrokenResource(jedis);
} finally {
returnResource(jedispool, jedis);
}
return rtn;
}
}
windows下redis的配置和jedis api的最基本的使用的更多相关文章
- Windows下Redis安装配置和使用注意事项
Windows下Redis安装配置和使用注意事项 一:下载 下载地址: https://github.com/microsoftarchive/redis/releases 文件介绍: 本文以3.2. ...
- windows下Redis主从复制配置(报错:Invalid argument during startup: unknown conf file parameter : slaveof)
主从复制配置中的遇到的异常: Invalid argument during startup: unknown conf file parameter : slaveof 把Redis文件夹复制两份 ...
- windows下安装和配置redis
1.windows下安装和配置redis 1.1 下载: 官网(linux下载地址):https://redis.io/ Windows系统下载地址:https://github.com/MSOpen ...
- windows下redis安装和配置
windows下redis安装和配置 redis介绍 Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. Redis有三个主要特点,使它优越于其它键值数 ...
- < python音频库:Windows下pydub安装配置、过程出现的问题及常用API >
< python音频库:Windows下pydub安装配置.过程出现的问题及常用API > 背景 刚从B站上看过倒放挑战之后也想体验下,心血来潮一个晚上完成了基本的实现.其中倒放与播放部分 ...
- windows下Redis安装及利用java操作Redis
一.windows下Redis安装 1.Redis下载 下载地址:https://github.com/MicrosoftArchive/redis 打开下载地址后,选择版本 然后选择压缩包 下载 R ...
- 最新Windows下Redis集群
实现简单的Windows下Redis集群配置,以下是配置过程中出现的几个问题: [1]逐个启动7001 7002 7003 7004 7005 7006节点时,出现createing server T ...
- Windows下Redis的使用
Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括st ...
- windows下Redis 主从读写分离部署
原文:windows下Redis 主从读写分离部署 1.可直接下载window下的运行文件(下面这个链接) 也可以浏览github 查看相应的版本说明文档 https://github.com/Ser ...
随机推荐
- hdu 3483 矩阵乘法
这个题目上周对抗赛题目,搞了我好久 对数学这种不是很敏感 其实都不是自己想出来的,看其他的资料和博客的推导 还是有点难度的,反正我是推不出来 通过二项式定理的化简 有两个博客写得比较好 http:// ...
- postman问题
问题1:postman请求出现 'SSL certificate verification' ,实际为https的问题 解决: 1)关闭fiddler 2)或在File > Settings & ...
- 第二季 第四天 part2
数据类型的转换 转化为字符串 String(value) 转型函数 这个转型函数能把任何类型的值转化为字符串 如果值有toString()方法 则用这个方法(调用没有参数的toString,默认十进制 ...
- 问:为什么java是单继承,但却是多实现的呢?
在学习的过程中,我发现了如题的这个有趣的问题. 单继承不必解释,一个类只能有一个直接父类:但是对于接口的实现,一个类却能够实现多个接口. 为什么是这种情况呢?我们来举个简单的栗子看一下: class ...
- springboot跨域请求接口示例
一.项目架构 二.项目内容 1.GlobalCrosConfig.java package com.config; import org.springframework.context.annotat ...
- h5-伪元素-before和after
做一个门票或者邮票:效果图 1.html就是两个div 2.具体css代码 <style> /*左侧长方体基本样式*/ div:nth-of-type(1){ width: 300px; ...
- array_flip() 函数返回一个反转后的数组
定义和用法 array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失. 如果原数组中的值的数据类型不是字符串或整数,函数将报错 ar ...
- iTOP-4412-Ubuntu系统源码-ubuntu没有声音的解决办法
准备工作 1.下载 vim 在命令行上输入 apt-get install vim 下载 vim 2.输入 vim /etc/hosts 在所打开界面的第一行最后写上 iTOP4412-ubuntu- ...
- 【@ConfigurationProperties注解】Not Found The requested URL /spring-boot/docs/2.2.2.RELEASE/reference/html/configuration-metadata.html was not found on this server.
<!-- 配置文件自动映射 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- UML-如何创建领域模型?
以当前迭代中所要设计的需求为界: 1.寻找概念类 2.将其绘制为类图 3.添加关联和属性