1.Install 2.QuickStart last
1.Install
- build:
make make V=1 #详细构建,输出更多的信息 make 32bit #构建32位运行环境的二进制目标 make test #构建后,测试一下 make distclean #清理
- install:
#方法1 #安装到 /usr/local/bin make install #方法2 #自定义安装目录 make PREFIX=/path/to/ install
- configure:
#获取配置文件 cp redis.conf /path/to/conf #配置项目 bind 192.168.0.244 port 6379 daemonize yes loglevel notice maxmemory 2mb maxmemory-policy allkeys-lru
- running:
cp redis.conf /path/to/conf cd /path/to # 启动方式1 bin/redis-server conf/redis.conf # 启动方式2 bin/redis-server --port 9999 #启动方式3 bin/redis-server --loglevel debug
默认配置是没有访问密码的,可以测试一下[root@localhost redis]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set user admin OK 127.0.0.1:6379> get user "admin" 127.0.0.1:6379> exit [root@localhost redis]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> get user "admin"
- 设置密码:配置文件、命令行;
requirepass develop123456789
验证密码:
[root@Develop2-Server01 redis]# redis-cli -h 192.168.0.244 192.168.0.244:6379> get foo (error) NOAUTH Authentication required. 192.168.0.244:6379> auth develop123456789 OK 192.168.0.244:6379> config get requirepass 1) "requirepass" 2) "develop123456789"
修改密码:
192.168.0.244:6379> config set requirepass 123 OK 192.168.0.244:6379> quit [root@Develop2-Server01 redis]# redis-cli -h 192.168.0.244 192.168.0.244:6379> auth 123 OK
- 服务托管
启动脚本
#! /bin/bash # # network Bring up/down networking # # chkconfig: 35 10 90 # description: Activates/Deactivates all network interfaces configured to \ # start at boot time. # case "$1" in start) /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf &>/dev/null ;; *) echo $"Usage: $0 {start}" esac exit 1
2.QuickStart
- 导包:spring-data-redis、jedis、slf4j-nop、junit、spring-test.
<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.pinyougou.demo.redis</groupId> <artifactId>redisdemo1</artifactId> <version>1.1</version> <properties> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> </dependencies> </project>
<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>cn.argor</groupId> <artifactId>redisdemo</artifactId> <version>1.1</version> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.6.RELEASE</version> </dependency> </dependencies> </project>
- 配置文件
- spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:property-placeholder location="classpath*:properties/*.properties" /> <!-- redis 相关配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="JedisConnectionFactory" /> </bean> </beans>
- redis
# Redis settings # server IP redis.host=127.0.0.1 # server port redis.port=6379 # server pass redis.pass= # use dbIndex redis.database=0 # \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B redis.maxIdle=300 # \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B redis.maxWait=3000 # \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684 redis.testOnBorrow=true
- log
### set æ¥å¿çº§å« - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=info, stdout ### è¾åºä¿¡æ¯å°æ§å¶æ¬ ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n ### è¾åºDEBUG 级å«ä»¥ä¸çæ¥å¿å°=E://logs/error.log ### #log4j.appender.D = org.apache.log4j.DailyRollingFileAppender #log4j.appender.D.File = E://logs/log.log #log4j.appender.D.Append = true #log4j.appender.D.Threshold = DEBUG #log4j.appender.D.layout = org.apache.log4j.PatternLayout #log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### è¾åºERROR 级å«ä»¥ä¸çæ¥å¿å°=E://logs/error.log ### #log4j.appender.E = org.apache.log4j.DailyRollingFileAppender #log4j.appender.E.File =E://logs/error.log #log4j.appender.E.Append = true #log4j.appender.E.Threshold = ERROR #log4j.appender.E.layout = org.apache.log4j.PatternLayout #log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
- spring
- 测试类
- 值类型:增加 boundValueOps().set()、查询 boundValueOps().get()
package test; import org.junit.runner.RunWith; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml") public class TestValue { @Autowired private RedisTemplate<String, String> redisTemp; @Test public void setValue() { redisTemp.boundValueOps("name").set("argor"); } @Test public void getValue() { String value = redisTemp.boundValueOps("name").get(); System.out.println(value); } @Test public void deleteValue() { redisTemp.delete("name"); } }
Set 集合:增加 boundSetOps().add()、查询 boundSetOps().members()
package test; import java.util.Set; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:*redis.xml") public class TestSet { @Autowired private RedisTemplate<String, String> redisTemp; @Test public void setValues() { redisTemp.boundSetOps("nameSet").add("root"); redisTemp.boundSetOps("nameSet").add("admin"); redisTemp.boundSetOps("nameSet").add("argor"); getValues(); } @Test public void getValues() { Set<String> set = redisTemp.boundSetOps("nameSet").members(); System.out.println(set); } @Test public void delValues() { redisTemp.delete("nameSet"); getValues(); } @Test public void delValue() { redisTemp.boundSetOps("nameSet").remove("root"); getValues(); } }
List 集合
package test; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:*redis.xml") public class TestList { @Autowired private RedisTemplate<String, String> redisTemp; @Test public void putRightValues() { redisTemp.boundListOps("nameListRight").rightPush("admin"); redisTemp.boundListOps("nameListRight").rightPush("root"); redisTemp.boundListOps("nameListRight").rightPush("argor"); getValuesRight(); } @Test public void getValuesRight() { Long len = redisTemp.boundListOps("nameListRight").size(); List<String> list = redisTemp.boundListOps("nameListRight").range(0, len - 1); System.out.println(list); } @Test public void putLeftValues() { redisTemp.boundListOps("nameListLeft").leftPush("sunny"); redisTemp.boundListOps("nameListLeft").leftPush("bruce"); redisTemp.boundListOps("nameListLeft").leftPush("tess"); getValuesLeft(); } @Test public void getValuesLeft() { Long len = redisTemp.boundListOps("nameListLeft").size(); List<String> list = redisTemp.boundListOps("nameListLeft").range(0, len - 1); System.out.println(list); } @Test public void destoryListRight() { destory("nameListRight"); getValuesRight(); } @Test public void destoryListLeft() { destory("nameListLeft"); getValuesLeft(); } public void destory(String nameList) { redisTemp.delete(nameList); } }
Hast 集合
package test; import java.util.List; import java.util.Set; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:*redis.xml") public class TestHash { @Autowired private RedisTemplate<String, String> redisTemp; @Test public void setValue() { redisTemp.boundHashOps("nameHash").put("name", "argor"); redisTemp.boundHashOps("nameHash").put("age", "33"); redisTemp.boundHashOps("nameHash").put("salary", "350"); getKeys(); getValues(); } @Test public void getKeys() { Set<Object> keys = redisTemp.boundHashOps("nameHash").keys(); System.out.println(keys); } @Test public void getValues() { Set<Object> keys = redisTemp.boundHashOps("nameHash").keys(); for (Object k: keys) { Object value = redisTemp.boundHashOps("nameHash").get(k); System.out.println(value); } } @Test public void destory() { redisTemp.delete("nameHash"); getKeys(); getValues(); } }
- 值类型:增加 boundValueOps().set()、查询 boundValueOps().get()
一切代码都是为了生活,一切生活都是调剂