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 ...
随机推荐
- Jenkins 插件中心国内镜像源发布
以下文章来源于Jenkins,作者LinuxSuRen Jenkins 社区的网络基础设施都是架设在国外的服务器上,而且,并没有在国内有 CDN 或者负载均衡的配置.对所有的 Jenkins 用户而言 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是 ...
- PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- MySQL--InnoDB 体系架构
InnoDB 体系架构 后台线程 Master Thread Master Thread 是一个非常核心的后台线程,主要负责将缓冲池中的数据异步刷新到磁盘,保证数据的一致性,包括脏页的刷新.合并插入缓 ...
- Vue.js——5.生命周期
Vue的生命周期 创建阶段new Vue1,beforeCreate() 表示在实例没有被创建出来之前会执行它加载data和methods2,caeated() data 和methods被初始化了 ...
- 启动zookeeper却没有进程
第一次: 没有jdk,安装好jdk就可以了 第二次: java的环境变量没配好,按照下图的配就行: Java_HOME和jre_HOME都是jdk的目录就行 最后两行不加试试,好像都没多大关系 应该是 ...
- $_SESSION $_COOKIE
$_SESSION是临时会话变量,用来储存访问者信息.内容是储存在服务器上面的.比如 $_SESSION["ABC"] = "aaa";那么这个用户访问时,$_ ...
- MyBatis从入门到精通(第6章):MyBatis 高级查询->6.1.1高级结果映射之一对一映射
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.IntelliJ IDEA 2019.2.4 本章主要包含的内容为 MyBatis 的高级结果映射,主要处理数据库一对一.一对多的 ...
- ZJNU 1223 - 素数距离——高级
因为最大可以达到int极限 明显直接筛选不可能完成 所以从其因子入手 因为任何不是素数的数都有除了1与其自身之外的因子 因此,我们筛出2^(31/2)≍46350之内的所有素数,以其作为因子再将题目给 ...
- Tensorflow学习教程------实现lenet并且进行二分类
#coding:utf-8 import tensorflow as tf import os def read_and_decode(filename): #根据文件名生成一个队列 filename ...