什么是 Redis
Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker.
安装 Redis
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz
$ tar xzf redis-3.0.7.tar.gz
$ cd redis-3.0.7
$ make
安装错误
A: /bin/sh: cc: command not found
Q: sudo apt-get install build-essential
A: jemalloc/jemalloc.h: No such file or directory
Q1:
cd deps
make hiredis lua jemalloc linenoise
运行验证
$ src/redis-server
配置 Redis
vi redis.conf
后台启动
./src/redis-server redis.conf &
jedis
官方推荐的 Java 客户端之一。
Maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>简单调用
package com.foamvalue.test;
import redis.clients.jedis.Jedis;
public class RedisTest {
public static void test() {
Jedis jedis = null;
try {
jedis = new Jedis("localhost", 6379);
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
}
public static void main(String[] args) {
test();
}
}