安装
tar -zxvf redis-3.2.5
cd redis-3.2.5
make
make install
make test
如果碰到安装问题
yum install gcc -y
make test碰到tcl问题
yum install tcl -y
启动
cd /usr/local/bin
./redis-server
带配置启动
修改配置文件
启用后台进程
daemonize yes
1.关闭protected-mode模式,注释bind
protected-mode no
#bind 127.0.0.1
2.开启protected-mode模式,配置bind及密码
protected-mode yes
bind 主机名
requirepass nozuonodie
验证
[root@h1 bin]# ./redis-cli -h 10.50.1.41
10.50.1.41:6379> get foo
(error) NOAUTH Authentication required.
10.50.1.41:6379> auth nozuonodie
OK
10.50.1.41:6379> get foo
"bar"
10.50.1.41:6379> set foo ljk
OK
10.50.1.41:6379> get foo
"ljk"
10.50.1.41:6379> exit
程序验证
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... strings) throws Exception {
String s = this.stringRedisTemplate.opsForValue().get("foo");
System.out.println(s);
}
}
打印结果为ljk
对象序列化验证
建立一个对象
public class User implements Serializable {
private static final long serialVersionUID = -892828255550795125L;
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
对象序列化接口
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
private static final byte[] EMPTY_ARRAY = new byte[0];
@Override
public byte[] serialize(Object o) throws SerializationException {
if (null == o) {
return EMPTY_ARRAY;
}
return serializer.convert(o);
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if (this.isEmpty(bytes)) {
return null;
}
return deserializer.convert(bytes);
}
private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
}
验证对象序列化是否成功
@SpringBootApplication
public class Application implements CommandLineRunner {
// @Autowired
// private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, User> redisTemplate;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... strings) throws Exception {
User u1 = new User("ljk", 18);
User u2 = new User("hwb", 3);
redisTemplate.opsForValue().set(u1.getName(), u1);
redisTemplate.opsForValue().set(u2.getName(), u2);
Integer age = redisTemplate.opsForValue().get("ljk").getAge();
System.out.println(age);
// String s = this.stringRedisTemplate.opsForValue().get("foo");
// System.out.println(s);
}
}