package com.phone.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.CriteriaDefinition;
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.phone.dto.Phone;
@Component
public class PhoneDao {
@Autowired
private MongoTemplate mongo;
@Autowired
private RedisTemplate<String, String> redis;
public List<String> getBrand() {
// TODO Auto-generated method stub
ListOperations<String, String> list = redis.opsForList();
List<String> range = list.range("brand", 0, -1);
return range;
}
public List<String> getContent() {
// TODO Auto-generated method stub
ListOperations<String, String> list = redis.opsForList();
List<String> range = list.range("content", 0, -1);
return range;
}
public void add(Phone phone) {
// TODO Auto-generated method stub
mongo.insert(phone);
}
public List<Phone> getlist() {
// TODO Auto-generated method stub
List<Phone> find = mongo.find(new Query(), Phone.class);
return find;
}
public void del(String[] id) {
// TODO Auto-generated method stub
mongo.remove(new Query(Criteria.where("id").in(id)),Phone.class);
}
public void update(Phone phone) {
// TODO Auto-generated method stub
Update update = new Update();
update.set("name", phone.getName());
update.set("price", phone.getPrice());
update.set("content", phone.getContent());
update.set("brand", phone.getBrand());
mongo.updateFirst(new Query(Criteria.where("id").is(phone.getId())), update, Phone.class);
}
public void badd(Phone phone) {
// TODO Auto-generated method stub
ListOperations<String, String> opsForList = redis.opsForList();
opsForList.leftPush("brand", phone.getBrand());
}
public void bdel(Phone phone) {
// TODO Auto-generated method stub
ListOperations<String, String> opsForList = redis.opsForList();
opsForList.remove("brand", 0, phone.getBrand());
}
public Phone getPhone(String id) {
// TODO Auto-generated method stub
return mongo.findOne(new Query(Criteria.where("id").is(id)), Phone.class);
}
public void like(String id, double hot) {
// TODO Auto-generated method stub
ZSetOperations<String, String> opsForZSet = redis.opsForZSet();
opsForZSet.incrementScore("phone", id, hot);
}
@Scheduled(cron="0/10 * * * * ?")
public void Task(){
ZSetOperations<String, String> zSet = redis.opsForZSet();
Set<TypedTuple<String>> rangeWithScores = zSet.rangeWithScores("phone", 0, -1);
for (TypedTuple<String> typedTuple : rangeWithScores) {
String id = typedTuple.getValue();
Double hot = typedTuple.getScore();
Phone phone = mongo.findOne(new Query(Criteria.where("id").is(id)), Phone.class);
if(phone!=null){
Update update = new Update();
update.set("hot", hot);
mongo.updateFirst(new Query(Criteria.where("id").is(id)), update, Phone.class);
}else{
zSet.remove("phone", id);
}
}
System.out.println("1");
System.out.println("Task Running...");
}
}
不要忘记人生是要战斗到死。
——芥川龙之介
本文详细介绍了一个结合Spring Data MongoDB和Redis的Java应用程序,通过示例代码展示了如何使用MongoDB进行数据持久化,同时利用Redis进行高效的数据缓存和操作。文章涵盖了数据增删改查、品牌和内容管理、热度更新及定时任务处理等核心功能。
1427

被折叠的 条评论
为什么被折叠?



