ssm redis操作类

接口:/**
 * 
 */
package com.hpe.ts.utils.other;

/**   
 * @Description:TODO描述:   
 * @author: 何思勇
 * @date:   2018年11月7日 上午10:05:28       
 */

public interface QuestionRedis <T>{

    /**
     * 
     * @Description:TODO描述:   Question存入redis
     * @author: 何思勇
     * @date:   2018年11月7日 上午11:47:36    
     * @param question
     * @return
     */
    void putQuestion(T question);
    
    /**
     * 
     * @Description:TODO描述:   从redis取出单个题
     * @author: 何思勇
     * @date:   2018年11月7日 上午10:10:57    
     * @param question
     * @return
     */
    T getQuestion(T question);
}
实现:

package com.hpe.ts.utils.other;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.hpe.ts.enums.CommonStatus;
import com.hpe.ts.pojo.ChoiceQuestion;

/**
 * 
 * @Description:TODO描述: 选择题redis操作类
 * @author: 何思勇
 * @date: 2018年11月6日 下午7:01:53
 */
@Component
public class ChoiceQuestionRedis implements QuestionRedis<ChoiceQuestion> {
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void putQuestion(ChoiceQuestion choiceQuestion) {
        redisTemplate.opsForValue().set("c:" + choiceQuestion.getCourseId() + ":" + choiceQuestion.getDifficulty() + ":"
                + choiceQuestion.getDisable() + ":" + choiceQuestion.getChoiceId(), choiceQuestion);
    }

    @Override
    public ChoiceQuestion getQuestion(ChoiceQuestion choiceQuestion) {

        return (ChoiceQuestion) redisTemplate.opsForValue()
                .get("c" + ":" + choiceQuestion.getCourseId() + ":" + choiceQuestion.getDifficulty() + ":"
                        + choiceQuestion.getDisable() + ":" + choiceQuestion.getChoiceId());
    }

    /**
     * 
     * @Description:TODO描述: 通配方法获取key集合根据课程号和难度进行模糊搜索
     * @author: 何思勇
     * @date: 2018年11月7日 下午2:10:49
     * @param courseId
     * @param difficulty
     * @param questionId
     * @param disable
     * @return
     */
    public Set<String> keys(Integer courseId, Integer difficulty, CommonStatus disable, Integer questionId) {
        String str = null;
        if (questionId != null) {
            str = "c" + ":" + "*" + questionId;
        } else {
            str = "c" + ":" + courseId + ":" + difficulty + ":" + disable + "*";
        }
        return redisTemplate.keys(str);
    }

    /**
     * 
     * @Description:TODO描述: 通配方法获取value集合根据课程号和难度进行模糊搜索时题号赋值null
     * @author: 何思勇
     * @date: 2018年11月7日 上午11:28:17
     * @param courseId
     * @param difficulty
     * @param questionId
     * @return
     */
    public List<Object> getQuestions(Integer courseId, Integer difficulty, Integer questionId) {
        Set<String> keys = keys(courseId, difficulty, CommonStatus.ENABLE, questionId);
        return redisTemplate.opsForValue().multiGet(keys);
    }

    /**
     * 
     * @Description:TODO描述: 更新redis中选择题的value
     * @author: 何思勇
     * @date: 2018年11月7日 下午9:35:11
     * @param str
     * @param cq
     */
    public void updataChoiceValue(String str, ChoiceQuestion cq) {
        redisTemplate.opsForValue().set(str, cq);
    }

    /**
     * @Description:TODO描述: 更新redis中选择题的key
     * @author: 何思勇
     * @date: 2018年11月7日 下午10:13:59
     * @param str
     * @param str1
     */

    public void updataKey(String oldKey, String newKey) {
        redisTemplate.rename(oldKey, newKey);
    }
}

 

猜你喜欢

转载自blog.csdn.net/Sir_He/article/details/83833707