什么是Redis?
Redis是一个可以持久化的缓存框架,支持分布式缓存,简单易用。类似的框架还有memcached,是一个Key-Value形式存储的缓存框架。
可以作为缓存的框架有:
EHCache
MongoDB 更偏向存储,而不是缓存
Redis 更偏向于缓存,而非存储
Memcached
Redis的安装和配置
1、先安装gcc
yum -y install gcc
redis的安装和使用步骤
步骤:
一、安装
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar xzf redis-3.2.9.tar.gz
cd redis-3.2.9
make MALLOC=libc
二、启动Redis服务
src/redis-server
如果要通过Java远程连接到Redis服务,还需要修改redis.conf文件
Java远程连接Linux服务器中的Redis
配置Linux服务器上的Redistribution服务,允许远程调用。
修改Redis的配置文档
在redis安装目录下的redis.conf文件中的如下内容:
1、注释掉redis安装目录下的redis.conf文件中的如下数据:bind 127.0.0.1,修改后为#bind 127.0.0.1
2、修改保护模式为非:默认为protected-mode yes ,修改后为protected-mode no
3、设置redis连接密码:找到#requirepass foobared ,在下面添加requirepass 123456
然后启动redis server:
./redis-server redis.conf
在以上过程中可能会需要重启redis server,终止和重启的命令如下:
1、终止,通过杀死redis的进程
kill -9 进程ID (解释:-9的含义是强制杀死)
进程ID可以通过如下命令查询:
ps -ef | grep 'redis'
三、启动客户端并测试
src/redis-cli
带密码的启动方式
./redis-cli -h 127.0.0.1 -p 6379 -a 123456
-h 是主机IP地址
-p 是端口号
-a 是密码
最后在Java代码中调用Redis。(使用Jedis方式调用)
1、导包:
1 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> 2 <dependency> 3 <groupId>redis.clients</groupId> 4 <artifactId>jedis</artifactId> 5 <version>2.9.0</version> 6 </dependency>
View Code
2、代码:
1 JedisShardInfo sharInfo = new JedisShardInfo("192.168.72.188", 6379); 2 sharInfo.setPassword("123456"); 3 Jedis jedis = new Jedis(sharInfo); 4 String result = jedis.set("name", "zhangsan"); 5 System.out.println(result); 6 String name = jedis.get("name"); 7 System.out.println(name); 8 jedis.close();
View Code
3、Java中Redis池
1 JedisPoolConfig config = new JedisPoolConfig(); 2 config.setMaxIdle(1000);//最大空闲时间 3 config.setMaxWaitMillis(1000); //最大等待时间 4 config.setMaxTotal(500); //redis池中最大对象个数 5 6 JedisShardInfo sharInfo = new JedisShardInfo("192.168.72.188", 6379); 7 sharInfo.setPassword("123456"); 8 List<JedisShardInfo> list = new ArrayList<JedisShardInfo>(); 9 list.add(sharInfo); 10 ShardedJedisPool pool = new ShardedJedisPool(config, list); 11 ShardedJedis jedis2 = pool.getResource(); 12 jedis2.set("haha", "测试"); 13 System.out.println(jedis2.get("haha"));
View Code
可能会出现的BUG:
redis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error. at redis.clients.jedis.Protocol.processError(Protocol.java:127) at redis.clients.jedis.Protocol.process(Protocol.java:161) at redis.clients.jedis.Protocol.read(Protocol.java:215) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) at redis.clients.jedis.Jedis.set(Jedis.java:121) at redis.clients.jedis.ShardedJedis.set(ShardedJedis.java:43) at com.javen.redis.RedisTest.testHelloWorld(RedisTest.java:29) 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 junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131) 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)
View Code
解决:
登录到redis客户端,然后输入如图红色方框中的命令:
config set stop-writes-on-bgsave-error no