淘淘商城21_项目改造02_添加缓存redis

本文介绍了一种利用Redis缓存来优化首页广告位数据加载效率的方法,通过在ContentServiceImpl中实现从Redis获取数据,若数据不存在则从MySQL查询并写入Redis,从而提升首页广告加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 创建配置文件resource.properties

#首页大广告位展示
CONTENT_INDEX_REDIS_KEY=CONTENT_INDEX_AD

2. 修改配置文件applicationContext-dao.xml

3. ContentServiceImpl.java

package com.taotao.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.content.dao.JedisClient;
import com.taotao.mapper.ContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.ContentService;
import com.taotao.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
import com.taotao.utils.TaotaoResult;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private ContentMapper contentMapper;
	
	@Value("${CONTENT_INDEX_REDIS_KEY}")
	private String CONTENT_INDEX_REDIS_KEY;//taotao-content-service中的resouce.properties
	@Value("${REST_BASIC_URL}")//与taotao-portal中的resources.properties对应上
	private String REST_BASIC_URL;
	@Value("${INDEX_CONTENT_URL}")
	private String INDEX_CONTENT_URL;
	
	@Override
	public List<TbContent> getContentByCategoryId(long categoryId) {
		//1.去redis数据库查询数据
		String jedisJson = jedisClient.hget(CONTENT_INDEX_REDIS_KEY, categoryId+"");
		//2.判断
		//3.若数据存在,则返回数据
		if (StringUtils.isNotEmpty(jedisJson)) {//redis不为空
			//将数据转换为list
			List<TbContent> list = JsonUtils.jsonToList(jedisJson, TbContent.class);
			return list;
		}
	
		//4.若数据不存在,则从MySQL数据库中查询数据,并将数据写入到redis中
		List<TbContent> contentlist = contentMapper.getContentByCategoryId(categoryId);
		
		//往缓存中添加数据,缓存中存储的value是字符串类型的
		jedisClient.hset(CONTENT_INDEX_REDIS_KEY, categoryId+"", JsonUtils.objectToJson(contentlist));
		return contentlist;
	}

}

4. IndexController.java

package com.taotao.portal.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.pojo.TbContent;
import com.taotao.portal.service.ContentService;
import com.taotao.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
import com.taotao.utils.TaotaoResult;

@Controller
public class IndexController {
	
	@Autowired
	private ContentService contentService;
	
	@RequestMapping("/index")
	public String indexPage(Model model){
		List<TbContent> content = contentService.getContentByCategoryId(89);
		 List list = new ArrayList<>();
		//遍历
		for (TbContent tbContent : content) {
			//用map拼接数据
			//{"srcB":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg","height":240,"alt":"","width":670,"src":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg","widthB":550,"href":"http://sale.jd.com/act/e0FMkuDhJz35CNt.html?cpdad=1DLSUE","heightB":240}
			Map map = new HashMap<>();
			map.put("srcB", tbContent.getPic());
			map.put("height",240 );
			map.put("alt", tbContent.getSubTitle());
			map.put("width",670 );
			map.put("src", tbContent.getPic2());
			map.put("widthB", 550);
			map.put("href", tbContent.getUrl());
			map.put("heightB", 240);
			
			//将map数据添加到list中
			list.add(map);
			
		}
		//将list转换为json
		String jsonData = JsonUtils.objectToJson(list);
		model.addAttribute("ad1", jsonData);//与index.jsp中的数据同步
		return "index";
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值