Redis简介-认识Redis
1、什么是redis:
redis是开源BSD许可高级的key-value存储系统(NoSQL),可以用来存储字符串,哈希结构,链表,集合,因此,常用来提供数据结构服务。
在Java程序中通过jedis连接Redis。
redis是一种高级的key_value的存储系统,其中value支持五中数据类型。
1、字符串(String)
2、哈希(hash)
3、字符串列表(list)
4、字符串集合(set)
5、有序字符串集合(sorted set)
所有的操作都是针对与Key关联的Value的。
redis是用c语言开发的一个开源的高性能键值对数据库。
2、简单使用
redis-cli.exe是客户端。连接Redis服务器后,就可以往redis服务器存数据。
需要使用一些命令。例如:ping,set/get,del等。
3、怎么用java连接redis
1.导入相应的jar包。
2.单实例连接:
public class JedisTest {
public void testJedis() {
Jedis jedis = testPool().getResource();
// Jedis jedis = new Jedis("192.168.20.112", 6379); //连接Redis
jedis.auth("123456");// 如果需密码
int i = 0;// 记录操作次数
try {
long start = System.currentTimeMillis();// 开始毫秒数
while (true) {
long end = System.currentTimeMillis();
if (end - start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作
break;
}
i++;
jedis.set("test" + i, i + "");
}
} finally {// 关闭连接
jedis.close();
}
System.out.println("redis每秒操作:" + i + "次");// 打印1秒内对Redis的操作次数
}
private JedisPool testPool() {
JedisPoolConfig poolCfg = new JedisPoolConfig();
// 最大空闲数
poolCfg.setMaxIdle(50);
// 最大连接数
poolCfg.setMaxTotal(100);
// 最大等待毫秒数
poolCfg.setMaxWaitMillis(20000);
// 使用配置创建连接池
JedisPool pool = new JedisPool(poolCfg, "192.168.20.112");
// 从连接池中获取单个连接
/*
* Jedis jedis = pool.getResource(); // 如果需密码 jedis.auth("123456");
*/
return pool;
}
}
redis就是一个服务器,无论是从redis-cli.exe客户端,还是通过Java代码提交的数据都存放在里面。
3.连接池连接:
// 1.获得连接池配置对象,设置配置项。
JedisPoolConfig config = new JedisPoolConfig();
// 1.1最大连接数。
config.setMaxTotal(200);
// 1.2最大空闲连接数。
config.setMaxIdle(50);
config.setMinIdle(8);// 设置最小空闲数
config.setMaxWaitMillis(10000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
// Idle时进行连接扫描
config.setTestWhileIdle(true);
// 表示idle object evitor两次扫描之间要sleep的毫秒数
config.setTimeBetweenEvictionRunsMillis(30000);
// 表示idle object evitor每次扫描的最多的对象数
config.setNumTestsPerEvictionRun(10);
// 表示一个对象至少停留在idle状态的最短时间,然后才能被idle object
// evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
config.setMinEvictableIdleTimeMillis(60000);
4、redis和memcached区别:
1、redis可以用来做存储(storage),而memcached是用来做缓存(cache)这个特点主要因为其有持久化功能。
2、redis中存储的数据有多种结构,而memcached存储的数据只有一种类型“字符串”。
5、redis相关特性:
1.多数据库:
一个redis实例可以包含多个数据库,客户端可以指定连接某个redis实例的那个数据库。
2、服务器命令
3、消息订阅与发布
4、redis事务
6、java连接虚拟机中的redis出现的问题,redis已经启动就是连不上
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:155)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:83)
at redis.clients.jedis.Connection.sendCommand(Connection.java:94)
at redis.clients.jedis.Connection.sendCommand(Connection.java:89)
at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:539)
at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2000)
at com.taotao.redis.RedisTest.testJedisSingle(RedisTest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
。。。。。。
原因:
检查redis的配置文件redis.conf,注释掉bind。

然后还会继续报错:
DENIED Redis is running in protected mode because protected mode is enabled…
需要设置密码,登录redis-cli
然后输入config set requirepass 123456 //123456是密码
开启外部客户端访问 redis 服务器的几种方式:
1、 bind 127.0.0.1 后直接添加 ip 地址,空格隔开;
2、注释掉 bind 127.0.0.1,并关闭保护模式: protected-mode no;
3、注释掉 bind 127.0.0.1,不修改默认保护模式,配置 requirepass yourpassword(你设置的密码), 以后客户端(以windows为例)可以通过命令 redis-cli.exe -h yourIP -p 6379 连接上服务器后输入 auth yourpassword(密码); 或 redis-cli.exe -h yourIP -p 6379 -a yourpassword(密码) 访问
Redis (error) NOAUTH Authentication required 解决方法:
输入密码即可解决 auth redis (redis是redis的密码)
127.0.0.1:6379> set mykey lisi
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> set mykey lisi
OK
127.0.0.1:6379> get mykey
"lisi"
198

被折叠的 条评论
为什么被折叠?



