赞
踩
访问官网地址下载安装:https://redis.io/
在redis安装路径中打开cmd窗口:
启动命令:redis-server redis.conf
执行命令后会展示如下页面,不要关闭连接
重新打开一个cmd窗口,输入如下命令启动Redis客户端,连接成功出现如下图页面
redis-cli.exe -h 127.0.0.1 -p 6379
<!--Jedis(Java 和 Redis 的混拼) 依赖--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--线程池--> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <!--序列化--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.48</version> </dependency> <!--google 类库--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.0.1-jre</version> </dependency>
public class JedisPoolUtils { public JedisPool jedisPool = null; public JedisPool getJedisPool(){ JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(8); config.setMaxIdle(2); config.setMinIdle(0); config.setMaxWaitMillis(3000); config.setTestOnBorrow(true); jedisPool = new JedisPool(config,"127.0.0.1", 6379); return jedisPool; } public Jedis getJedis(){ JedisPool jedisPool = getJedisPool(); if (jedisPool != null){ return jedisPool.getResource(); }else { return null; } } public void close(Jedis jedis){ if (jedis != null){ jedis.close(); } } }
public class RedisDemo { public static void main(String[] args) throws IOException { //工具类访问 JedisPoolUtils jedisPoolUtils = new JedisPoolUtils(); Jedis jedis = jedisPoolUtils.getJedis(); test1(jedis); test2(jedis); test3(jedis); test4(jedis); test5(jedis); test6(jedis); test7(jedis); test8(jedis); test9(jedis); jedisPoolUtils.close(jedis); }
String
private static void test1(Jedis jedis) { jedis.set("username", "tom"); System.out.println(jedis.get("username")); //单次添加多个数据 jedis.mset("password", "123", "age", "23"); List<String> values = jedis.mget("username", "password", "age"); System.out.println("values = " + values); //演示append,追加数据 jedis.append("username", "is boy"); System.out.println(jedis.get("username")); //替换数据 jedis.setrange("username", 7, "girl"); System.out.println(jedis.get("username")); //范围查询 System.out.println(jedis.getrange("username", 0, -1)); }
List
//list
private static void test2(Jedis jedis) {
jedis.select(1);
jedis.lpush("names", "张三", "李四", "tom", "john");
List<String> name = jedis.lrange("names", 0, -1);
System.out.println("names = " + name);
//指定获取索引数据,会遍历循环整个list
System.out.println(jedis.lindex("names", 1));
//在tom之前插入steve数据
jedis.linsert("names", BinaryClient.LIST_POSITION.BEFORE, "tom", "steve");
System.out.println(jedis.lrange("names", 0, -1));
jedis.lrem("names", 2, "tom");
System.out.println(jedis.lrange("names", 0, -1));
jedis.del("names");
hash
private static void test3(Jedis jedis) { jedis.hset("user", "username", "tom"); System.out.println(jedis.hget("user", "username")); Map<String, String> map = new HashMap<String, String>(); map.put("password", "123"); map.put("age", "15"); map.put("sex", "male"); map.put("username", "james"); jedis.hmset("user", map); List<String> hmget = jedis.hmget("user", "username", "sex", "age", "password"); System.out.println("hmget = " + hmget); Map<String, String> user = jedis.hgetAll("user"); for (String key : user.keySet()) { System.out.println(key + ":" + map.get(key)); } Set<String> user1 = jedis.hkeys("user"); System.out.println("user1 = " + user1); List<String> user2 = jedis.hvals("user"); System.out.println("user2 = " + user2); jedis.hdel("user", "username", "password"); Map<String, String> user3 = jedis.hgetAll("user"); System.out.println(user3); for (String key : user3.keySet()) { System.out.println(key + ":" + map.get(key)); } }
Set
private static void test4(Jedis jedis) { jedis.sadd("language", "java", "c", "python", "matlab"); System.out.println(jedis.smembers("language")); jedis.srem("language", "c"); System.out.println(jedis.smembers("language")); jedis.sadd("l1", "java", "c", "IOS", "android"); jedis.sadd("l2", "java", "python", "c++"); //以第一个key为准,获取出在第二个key中不存在的数据 Set<String> sdiff = jedis.sdiff("l1", "l2"); System.out.println("sdiff = " + sdiff); Set<String> sdiff1 = jedis.sdiff("l2", "l1"); System.out.println("sdiff1 = " + sdiff1); //获取两个key中相同的元素 Set<String> sinter = jedis.sinter("l1", "l2"); System.out.println("sinter = " + sinter); //获取两个key的交集 Set<String> sunion = jedis.sunion("l1", "l2"); System.out.println("sunion = " + sunion); }
sorted
private static void test5(Jedis jedis) { Map<String, Double> map = new HashMap<String, Double>(); map.put("张三", 18.00); map.put("李四", 19.00); map.put("王五", 17.23); map.put("马六", 19.02); jedis.zadd("zkey", map); System.out.println(jedis.zrange("zkey", 0, -1)); //指定分数范围查询 System.out.println(jedis.zrangeByScore("zkey", 17, 18)); Set<Tuple> zkey = jedis.zrangeWithScores("zkey", 0, -1); for (Tuple tuple : zkey) { System.out.println(tuple.getScore() + " : " + tuple.getElement()); } System.out.println("========================="); //分数排名 System.out.println(jedis.zrank("zkey", "李四")); //获取分数 System.out.println(jedis.zscore("zkey", "李四")); jedis.zrem("zkey", "李四"); System.out.println(jedis.zrange("zkey", 0, -1)); }
Set+过期时间
private static void test6(Jedis jedis) {
Set<String> keys = jedis.keys("*");
System.out.println("keys = " + keys);
jedis.del("user");
System.out.println(keys);
jedis.expire("username", 200);
Long username = jedis.ttl("username");
System.out.println(username);
jedis.persist("username");
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。