<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
public class JedisTestor { public static void main(String[] args) { Jedis jedis = new Jedis("192.168.86.128",6379); try{ jedis.auth("123456"); jedis.select(2); System.out.println("success"); jedis.mset(new String[]{"title","奶粉","num","20"}); List<String> mget = jedis.mget(new String[]{"sn", "title", "num"}); System.out.println(mget); Long num = jedis.incr("num"); System.out.println(num); jedis.hset("student:3312","name","小明"); String name = jedis.hget("student:3312", "name"); System.out.println(name); Map<String,String> stu = new HashMap(); stu.put("name","lili"); stu.put("age","18"); stu.put("id","3313"); jedis.hmset("student:3313",stu); Map<String, String> smap = jedis.hgetAll("student:3313"); System.out.println(smap); jedis.del("letter"); jedis.rpush("letter",new String[]{"d","e","f"}); jedis.lpush("letter",new String[]{"c","b","a"}); List<String> letter = jedis.lrange("letter", 0, -1); jedis.lpop("letter"); jedis.rpop("letter"); letter = jedis.lrange("letter", 0, -1); System.out.println(letter); }catch (Exception e){ e.printStackTrace(); }finally { jedis.close(); } } }
public class CacheSample { public CacheSample(){ Jedis jedis = new Jedis("192.168.86.128",6379); try { List<Goods> goodsList = new ArrayList<>(); goodsList.add(new Goods(8818,"pg","",3.5f)); goodsList.add(new Goods(8819,"xj","",5f)); goodsList.add(new Goods(8820,"cz","",25f)); jedis.auth("123456"); jedis.select(3); for(Goods goods:goodsList){ String json = JSON.toJSONString(goods); System.out.println(json); String key = "goods"+goods.getGoodsId(); jedis.set(key,json); } }catch (Exception e){ e.printStackTrace(); }finally { jedis.close(); } } public static void main(String[] args) { new CacheSample(); System.out.println("输入要查询的商品编号"); String goodsId = new Scanner(System.in).next(); Jedis jedis = new Jedis("192.168.86.128",6379); try{ jedis.auth("123456"); jedis.select(3); String key = "goods"+goodsId; if(jedis.exists(key)){ String s = jedis.get(key); System.out.println(s); Goods g = JSON.parseObject(s, Goods.class); System.out.println(g.getGoodsName()); }else{ System.out.println("编号错误"); } }catch (Exception e){ e.printStackTrace(); }finally { jedis.close(); } } }