8月25日 分布式缓存 周六

package com.dao;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dto.Lesson;
import com.dto.PageUtils;

@Component
public class LessonDao {
    @Autowired
    private MongoTemplate mongo;
    @Autowired
    private RedisTemplate redis;
    public List toAdd() {
        // TODO Auto-generated method stub
        ListOperations opsForList = redis.opsForList();
//      opsForList.leftPush("lessontype", "摄影");
//      opsForList.leftPush("lessontype", "书法");
//      opsForList.leftPush("lessontype", "棋牌");
//      opsForList.leftPush("lessontype", "传统文化");
        List range = opsForList.range("lessontype", 0, -1);
        return range;
    }
    public void add(Lesson lesson) {
        // TODO Auto-generated method stub
        mongo.insert(lesson);
    }
    public List findList(PageUtils pu, String mohu) {
        // TODO Auto-generated method stub
        List find = mongo.find(new Query(Criteria.where("lessonname").regex(mohu)).skip(pu.getStartIndex()).limit(pu.getTotalPage()), Lesson.class);
        return find;
    }
    public Lesson getObj(String id) {
        // TODO Auto-generated method stub
        Lesson lesson = mongo.findOne(new Query(Criteria.where("id").is(id)), Lesson.class);
        return lesson;
    }
    public void update(Lesson lesson) {
        // TODO Auto-generated method stub
        Update update = new Update();
        update.set("lessontype", lesson.getLessontype());
        update.set("lessonname", lesson.getLessonname());
        update.set("teacher", lesson.getTeacher());
        update.set("content", lesson.getContent());
        update.set("starttime", lesson.getStarttime());
        update.set("endtime", lesson.getEndtime());
        mongo.updateFirst(new Query(Criteria.where("id").is(lesson.getId())), update, Lesson.class);
    }
    public void del(String id) {
        // TODO Auto-generated method stub
        mongo.remove(new Query(Criteria.where("id").is(id)), Lesson.class);
    }
    public void score(String id) {
        // TODO Auto-generated method stub
        ZSetOperations opsForZSet = redis.opsForZSet();
        opsForZSet.incrementScore("lesson", id, 1);
    }

    @Scheduled(cron="0/5 * * * * ?")
    public void tt(){
        ZSetOperations opsForZSet = redis.opsForZSet();
        Set> rangeWithScores = opsForZSet.rangeWithScores("lesson", 0, -1);
        for (TypedTuple typedTuple : rangeWithScores) {
            String value = typedTuple.getValue();
            Double score = typedTuple.getScore();
            Lesson lesson = mongo.findOne(new Query(Criteria.where("id").is(value)), Lesson.class);
            if(lesson!=null){
                Update update = new Update();
                update.set("hot", score);
                mongo.updateFirst(new Query(Criteria.where("id").is(value)), update, Lesson.class);
                System.out.print(lesson.getLessonname()+" "+lesson.getHot());
            }else{
                opsForZSet.remove("lesson", Lesson.class);
            }
        }

        System.out.println("Running ...");
    }
    public Integer getCount() {
        // TODO Auto-generated method stub
        long count = mongo.count(new Query(), Lesson.class);
        return (int) count;
    }

}

茫茫人海,如果你不够特别,很容易就会被遗忘呢。
——黑心苹果《重生之爷太重口了》

你可能感兴趣的:(2018)