redis在windows的安装使用

1. windows下安装redis

1.1. 安装

直接百度搜索redis的windows版本
我下的版本是: Redis-x64-3.2.100.zip
(之后所有需要的东西我都会上传一份,包括这个压缩包,以及客户端工具)
解压后是这样
解压之后的图

1.2. 修改密码

在redis.windows.conf下 在大概444行左右
修改密码

2. windows操作命令

在解压包路径下启动 redis-server redis.windows.conf
服务启动后不要关闭cmd窗口,保持服务

启动

redis-cli.exe -h 127.0.0.1 -p 6379

验证密码

auth 123456

选择db

select 0

插入

set name 123

(其实上边命令输入完都有提示的,关于复杂命令可以详细看文档)
这里写图片描述

3. redis客户端软件(windows)

这里写图片描述
客户端见文章尾部压缩包

4. 搭建项目

4.1. maven的pom文件

maven直接配置最新的jedis就ok

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wk</groupId>
	<artifactId>redis-01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
	</dependencies>
</project>

4.2. 样例代码

package jedis;
import java.util.HashMap;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class test01 {
	    static Jedis jedis = null;
	public static void main(String[] args) {
		jedis = getConnection();
		jedis.set("name", "zhangsan");
		System.out.println(jedis.get("name"));
		System.out.println(jedis.select(1));
		jedis.set("name", "zhangsan");
		jedis.mset("gender", "女", "age", "18");
		jedis.mset("gender", "男", "age", "21");
		jedis.lpush("dog1", "旺财", "阿黄", "小强");
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", "tmall");
		map.put("age", "2 month old");
		jedis.hmset("a", map);
		System.out.println(jedis.hgetAll("a"));
	}

	/**
	 * 获取连接
	 * 
	 * @MethodName getConnection
	 * @Description
	 * @return Jedis
	 * @Throws
	 * @Author wk
	 * @Date 2018年3月5日下午11:06:19
	 */
	public static Jedis getConnection() {
		jedis = new Jedis("127.0.0.1", 6379);
		// 设置密码
		jedis.auth("123456");
		return jedis;
	}

	/**
	 * 测试连接
	 * 
	 * @MethodName testConn
	 * @Description void
	 * @Throws
	 * @Author wk
	 * @Date 2018年3月5日下午11:06:01
	 */
	public void testConn() {
		String response = jedis.ping();
		System.out.println();
	}
}

4.3. redis连接池配置

package util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtils {
	private static JedisPool pool;

	/**
	 * 建立连接池
	 * @MethodName createJedisPool
	 * @Description void
	 * @Throws 
	 * @Author wk
	 * @Date 2018年3月5日下午11:14:50
	 */
	private static void createJedisPool() {
		// 建立连接池配置参数
		JedisPoolConfig config = new JedisPoolConfig();		
		// 设置最大连接数
		config.setMaxTotal(100);		
		// 设置空间连接
		config.setMaxIdle(10);;
		// 设置最大阻塞时间
		config.setMaxWaitMillis(1000);	
		// 创建连接池
		pool = new JedisPool(config, "127.0.0.1", 6379, 1000, "123456");		
		
	}
	
	/**
	 * 在多线程环境同步初始化
	 * @MethodName poolInit
	 * @Description void
	 * @Throws 
	 * @Author wk
	 * @Date 2018年3月5日下午11:15:06
	 */
	private static synchronized void poolInit() {
		if (pool == null) {
			createJedisPool();
		}
	}
	
	/**
	 * 获取一个jedis对象
	 * @MethodName getJedis
	 * @Description
	 * @return Jedis
	 * @Throws 
	 * @Author wk
	 * @Date 2018年3月5日下午11:15:24
	 */
	public static Jedis getJedis() {
		if (pool == null) {
			poolInit();
		}
		return pool.getResource();
	}
	
	/**
	 * 归还一个连接
	 * @MethodName returnRes
	 * @Description
	 * @param jedis void
	 * @Throws 
	 * @Author wk
	 * @Date 2018年3月5日下午11:15:43
	 */
	public static void returnRes(Jedis jedis) {
		pool.returnResource(jedis);
	}
}

下载路径:http://download.csdn.net/download/do_finsh/10270700

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值