官网没有window版的,这里可以window版本下载
https://github.com/tporadowski/redis/releases
官网下载
https://download.redis.io/releases/
redis-server.exe
运行服务,redis-cli.exe
运行客户端遇到不会的,就在客户端打help
set key value
get key
set key newval nx
set key newval xx
incr num
incrby num val
getset num newval
mset key1 val1 key2 val2 key3 val3
mget key1 key2 key3
exists key
del key
type key
expire key seconds
set key val ex seconds
ttl key
pexpire key millisecond
set key val px millisecond
pttl key
lpush key val1 val2 val3
rpush key val1 val2 val3
lrange start end
lpop key
rpop key
ltrim key satrt end
blpop key1 key2 seconds
brpop key1 kye2 seconds
llen key
ps:操作方式挺像hbase
hset key filed value
hget key field
hmset key fileld1 val1 fileld2 val2
hmget key field1 fileld2
hincrby key fileld increment
sadd key val1 val2 val3
smembers key
sismember key member
spop key count
sinter key1 key2 key3
sunion key1 key2 key3
sinterstore resultset key1 key2 key3
scard key
srandmember key count
zadd key score val
zrange key start end
zrevrange key start end
zrange key start end withscores
zrangebyscore key minscore maxscore
zremrangebyscore key startscore endscore
zrank key val
setbit key offset val
getbit key offset
bitop [and|or|xor|not] resultkey key1 key2 key3
bitcount key
bitpos key bit start end
pfadd key val1 val2 val3
pfcount key
pfmerge resultkey key1 key2 key3
jedis-2.9.0.jar
import redis.clients.jedis.Jedis;
public class RedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("hello", "world");
System.out.println(jedis.get("hello"));
}
}
1.导包
jedis-2.9.0.jar
spring-data-redis-1.6.4.RELEASE.jar
2. 配置文件application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="poolConfig" ref="poolConfig" />
bean>
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
<property name="defaultSerializer" ref="stringRedisSerializer" />
bean>
beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class TestString {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("/db/redis/applicationContext.xml");
RedisTemplate<String, String> redisTemplate = app.getBean(RedisTemplate.class);
redisTemplate.opsForValue().set("key1", "value1");
redisTemplate.opsForValue().set("key2", "value2");
//获取
String getKey1 = redisTemplate.opsForValue().get("key1");
System.out.println(getKey1);
//字符串长度
Long size = redisTemplate.opsForValue().size("key1");
System.out.println("字符串长度:" + size);
//设置新值并返回旧值
String oldValue = redisTemplate.opsForValue().getAndSet("key1", "new_value1");
System.out.println("旧值:" + oldValue);
getKey1 = redisTemplate.opsForValue().get("key1");
System.out.println("新值:" + oldValue);
//子串
getKey1 = redisTemplate.opsForValue().get("key1", 0, 5);
System.out.println(getKey1);
//字符串拼接
redisTemplate.opsForValue().append("key1", "_app");
getKey1 = redisTemplate.opsForValue().get("key1");
System.out.println(getKey1);
app.close();
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
public class TestSpring {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("/db/redis/applicationContext.xml");
RedisTemplate redisTemplate = app.getBean(RedisTemplate.class);
//设置
Role role = new Role();
role.setRoleName("hello");
redisTemplate.opsForValue().set("r1", role);
//获取
Role getRole = (Role)redisTemplate.opsForValue().get("r1");
System.out.println(getRole);
//SessionCallback可以保证方法内所有redis语句都由同一个redis连接执行
SessionCallback<Role> sc = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations rop) throws DataAccessException {
rop.opsForValue().set("r2", role);
return (Role) rop.opsForValue().get("r2");
}
};
Role r2 = (Role) redisTemplate.execute(sc);
System.out.println(r2);
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class TestHash {
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("/db/redis/applicationContext.xml");
RedisTemplate<String, String> redisTemplate = app.getBean(RedisTemplate.class);
String KEY = "hash";
HashMap<String,String> map = new HashMap<String, String>();
map.put("sj", "88");
map.put("wj", "7floor");
//设置值
redisTemplate.opsForHash().putAll(KEY, map);
//设置单个值
redisTemplate.opsForHash().put(KEY, "student", "yxd");
//遍历所有键和值
Map<Object, Object> entries = redisTemplate.opsForHash().entries(KEY);
for (Map.Entry<Object, Object> entry : entries.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
//增加值
redisTemplate.opsForHash().increment(KEY, "sj", 2.2);
String sj = (String)redisTemplate.opsForHash().get(KEY, "sj");
System.out.println(sj);
//删除值
redisTemplate.opsForHash().delete(KEY, "student");
//遍历所有值
List<Object> values = redisTemplate.opsForHash().values(KEY);
for (Object v : values){
System.out.println(v);
}
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class TestHyperLogLog {
public static void main(String[] args) {
ClassPathXmlApplicationContext app =
new ClassPathXmlApplicationContext("/db/redis/applicationContext.xml");
RedisTemplate<String, String> redisTemplate = app.getBean(RedisTemplate.class);
//设置
redisTemplate.opsForHyperLogLog().add("log", "a", "b", "c", "d", "a");
redisTemplate.opsForHyperLogLog().add("log2", "a");
redisTemplate.opsForHyperLogLog().add("log2", "z");
//获取不同元素的个数
Long log = redisTemplate.opsForHyperLogLog().size("log");
Long log2 = redisTemplate.opsForHyperLogLog().size("log2");
System.out.println(log);
System.out.println(log2);
//合并
redisTemplate.opsForHyperLogLog().union("new_log", "log", "log2");
Long new_log = redisTemplate.opsForHyperLogLog().size("new_log");
System.out.println(new_log);
}
}
watch:
在事务之前(multi)使用,
用法: watch key
结果: 如果观察的key的value在事务里被改变了,那么事务就会回滚
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
public class Tx {
public static void main(String[] args) {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate template = app.getBean(RedisTemplate.class);
SessionCallback callback = new SessionCallback() {
@Override
public Object execute(RedisOperations ro) throws DataAccessException {
//开启事务,创建命令队列
ro.multi();
//加入命令队列
ro.boundValueOps("k1").set("v1");
String k1 = (String) ro.boundValueOps("k1").get();
//执行命令队列的命令,有返回值
ro.exec();
// 这里只能以这种方式拿, 返回k1得到为null,为甚么
// 因为exec之前都没执行命令
return ro.boundValueOps("k1").get();
}
};
String v1 = (String) template.execute(callback);
System.out.println(v1);
}
}
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
public class RedisLine {
public static void main(String[] args) {
Jedis jedis = new Jedis();
//开启流水线
Pipeline line = jedis.pipelined();
for (int i = 0; i < 100000; i++) {
line.set("k" + i, "v" + i);
line.get("k" + i);
}
//流水线执行
line.syncAndReturnAll();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import java.util.concurrent.TimeUnit;
public class Ex {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate template = app.getBean(RedisTemplate.class);
SessionCallback callback = new SessionCallback() {
@Override
public Object execute(RedisOperations ro) throws DataAccessException {
ro.boundValueOps("rg1").set("value rg1");
// 10秒的存活时间
Boolean succeedSetExpire = ro.expire("rg1", 10, TimeUnit.SECONDS);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获取剩余时间
Long expireTime = ro.getExpire("rg1");
System.out.println(expireTime);
//持久化,把过期时间去掉
Boolean isSucceedPersist = ro.persist("rg1");
//获取剩余时间, -1代表永久
expireTime = ro.getExpire("rg1");
System.out.println(expireTime);
return null;
}
};
template.execute(callback);
}
}
从机只能读数据, 主机能读能写
./redis-server 配置文件