8月16日 分布式缓存 周四

package com.redis.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.redis.dto.User;

@Component
public class UserDao {
    @Autowired
    private RedisTemplate redis;
    @Autowired
    private RedisTemplate rediss;

    public List getHobby() {
        ListOperations opsForList = rediss.opsForList();
//      opsForList.leftPush("hobby", "抽烟");
//      opsForList.leftPush("hobby", "喝酒");
//      opsForList.leftPush("hobby", "烫头");
//      opsForList.leftPush("hobby", "纹身");

        List range = opsForList.range("hobby", 0, -1);
        return range;
    }

    public List getClazz() {
        ListOperations opsForList = rediss.opsForList();
//      opsForList.leftPush("clazz", "1605A");
//      opsForList.leftPush("clazz", "1605B");
//      opsForList.leftPush("clazz", "1605C");
//      opsForList.leftPush("clazz", "1605D");

        List range = opsForList.range("clazz", 0, -1);
        return range;
    }

    public void add(User user) {
        // TODO Auto-generated method stub
        HashOperations opsForHash = redis.opsForHash();
        opsForHash.put("user", user.getId(), user);
    }

    public List list() {
        // TODO Auto-generated method stub
        List list = new ArrayList();
        HashOperations opsForHash = redis.opsForHash();
        Collection values = opsForHash.entries("user").values();
        for (User user : values) {
            list.add(user);
        }
        return list;
    }

    public void del(String id) {
        // TODO Auto-generated method stub
        HashOperations opsForHash = redis.opsForHash();
        opsForHash.delete("user", id);
    }

    public User getUser(String id) {
        // TODO Auto-generated method stub
        HashOperations opsForHash = redis.opsForHash();
        User user = opsForHash.get("user", id);
        return user;
    }

    public void update(User user) {
        // TODO Auto-generated method stub
        HashOperations opsForHash = redis.opsForHash();
        opsForHash.put("user", user.getId(), user);
    }
}

只是遇见
不谈余生

你可能感兴趣的:(2018)