首页 > 代码库 > Spring RedisTemplate操作-String操作(2)

Spring RedisTemplate操作-String操作(2)

@Autowired
    @Resource(name="redisTemplate")
    private RedisTemplate<String, String> rt;
    
    public void flushdb(){
        rt.execute(new RedisCallback<Object>() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
    
    public void set(){
        flushdb();
        ValueOperations<String, String> vo = rt.opsForValue();
        vo.set("wzg", "nihao");
        vo.set("wzg", "nihao",3);
        vo.set("wzg2", "nihao",2,TimeUnit.MINUTES);
        vo.set("mc", "nihao");
        vo.setIfAbsent("wzg", "dsg");
        Map<String, String> map = new HashMap<String, String>();
        map.put("cff", "xsg");
        map.put("cl", "xxsg");
        vo.multiSet(map);
        vo.append("cff", " sm");
        vo.increment("age", 2);
        vo.increment("age", 1);
        vo.increment("age", -1);
        rt.delete("mc");
        rt.convertAndSend("redisss", "niyashabi");
        rt.setEnableTransactionSupport(true);
    }
    
    public void get(){
        ValueOperations<String, String> vo = rt.opsForValue();
        out(vo.get("wzg"));
        out(vo.get("wzg",1,2));
        out(vo.getAndSet("wzg", "hello"));
        out(vo.get("wzg"));
        out(vo.multiGet(Arrays.asList("cff","cl")));
//        out(vo.get("age"));
        out(vo.size("wzg"));
        out(vo.get("mc"));
        out(rt.keys("*"));
        out(rt.randomKey());
        out(rt.getClientList());
        out(rt.hasKey("wzg"));
        out(rt.type("wzg"));
        
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//        }
//        out(rt.getExpire("wzg2"));
    }

 

Spring RedisTemplate操作-String操作(2)