day53.品优购电商,redis分布式缓存简介和适用情况,逻辑删除的定义,springDateRedis示例,电商广告运用redis操作细节,

redis分布式缓存

redis底层使用C语言编写, 存储数据是放在内存中, 速度非常快.
作用: 
	redis在互联网项目中一般充当分布式缓存使用
业务流程:
	获取数据的时候先从redis中获取, 如果获取到数据则直接返回, 就不用访问数据库了
	如果获取不到数据, 可以从数据库中查询, 查询到后放入redis中一份, 下回就可以直接从redis中查询到
	这样大大降低了数据库的高并发访问压力.
持久化方案:
	a) rdb(默认) 分时持久化
		可以在配置文件中设定, 多长时间持久化一次, 持久化次数少也就是操作硬盘的次数少,
		速度快. 但是如果在没有完成持久化前, 如果服务器断电, 则内存中没有持久化的数据会丢失.

b) aof 实时持久化
	每次向redis中做增删改操作, 都会将数据持久化到硬盘上, 数据可靠性高, 不会丢失,
	但是速度慢

redis中数据类型: string, set, zset, list, hash
redis同类型技术:
	memcache是redis的同类型技术, 底层也是使用c语言编写, 现在memcache已经被redis替代了.
	memcache的速度和redis相当. 但是memcache没有持久化方式.
mongodb和redis区别:
	mongodb也是一个nosql数据库, 存储的数据是非结构化的, 但是mongdb和redis不能放在一起对比,
	因为完全没有可比性, 使用场景完全不同
	redis: 主要使用内存, 有两种持久化方案, 速度非常快, 一般做分布式缓存使用
	mongodb: 主要使用硬盘存储, 所以不会担心数据丢失, 速度介于redis和传统数据库之间.
		但是mongodb更擅长存储大文本数据, 以及一些非结构化数据, mongodb比redis的数据类型更加丰富.
		例如: 存储小说网站的小说, 存储电商网站的评论等这些数据

逻辑删除的定义

4.运营商后台-商品管理【商品删除】
4.1需求分析
我们为商品管理提供商品删除功能,用户选中部分商品,点击删除按钮即可实现商品删除。注意,这里的删除并非是物理删除,而是修改tb_goods表的is_delete字段为1 ,我们可以称之为“逻辑删除”

redis再linux中的安装步骤,和示例,具体参照redis文档,点击到超链接

电商广告运用redis操作细节

redis中的五大数据类型:

string:  字符串
	hash:	相当于map, 是一种键值对形式, 存入的数据是无序的, key不可以重复
	list:	存入其中的数据是有序的, 存入其中的数据可以重复
	set:	存入其中的数据是无序的, 存入其中的数据不可以重复
	zset:	存入其中的数据是有序的, 存入其中的数据不可以重复

执行crud操作时,要对redis 执行的相关更新操作

/**
 * 使用redis分布式缓存原则:
 * 一般关系型数据库作为我们的主数据库存储数据, 如果涉及到使用分布式缓存redis, 要保证redis中的数据
 * 和数据库中的数据要一致.
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentDao contentDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Content> findAll() {
        return contentDao.selectByExample(null);
    }

    @Override
    public PageResult findPage(Content content, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        ContentQuery query = new ContentQuery();
        ContentQuery.Criteria criteria = query.createCriteria();
        if (content != null) {
            if (content.getTitle() != null && !"".equals(content.getTitle())) {
                criteria.andTitleLike("%"+content.getTitle()+"%");
            }
        }
        Page<Content> contentList = (Page<Content>)contentDao.selectByExample(query);
        return new PageResult(contentList.getTotal(), contentList.getResult());
    }

    @Override
    public void add(Content content) {
        //1. 将新广告添加到数据库中
        contentDao.insertSelective(content);
        //2. 根据分类id, 到redis中删除对应分类的广告集合数据
        redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).delete(content.getCategoryId());
    }

    @Override
    public Content findOne(Long id) {
        return contentDao.selectByPrimaryKey(id);
    }

    @Override
    public void update(Content content) {
        //1. 根据广告id, 到数据库中查询原来的广告对象
        Content oldContent = contentDao.selectByPrimaryKey(content.getId());
        //2. 根据原来的广告对象中的分类id, 到redis中删除对应的广告集合数据
        redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).delete(oldContent.getCategoryId());
        //3. 根据传入的最新的广告对象中的分类id, 删除redis中对应的广告集合数据
        redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).delete(content.getCategoryId());
        //4. 将新的广告对象更新到数据库中
        contentDao.updateByPrimaryKeySelective(content);
    }

    @Override
    public void delete(Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                //1. 根据广告id, 到数据库中查询广告对象
                Content content = contentDao.selectByPrimaryKey(id);
                //2. 根据广告对象中的分类id, 删除redis中对应的广告集合数据
                redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).delete(content.getCategoryId());
                //3. 根据广告id删除数据库中的广告数据
                contentDao.deleteByPrimaryKey(id);
            }
        }
    }

    @Override
    public List<Content> findByCategoryId(Long categoryId) {
        ContentQuery query = new ContentQuery();
        ContentQuery.Criteria criteria = query.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<Content> list = contentDao.selectByExample(query);
        return list;
    }

    /**
     * 整个redis相当于一个大的hashMap, 在这个map中key是不可以重复的, 所以key是稀缺资源
     * key      value(value使用hash类型因为可以尽量少的占用key)
     *          field           value
     *          分类id            对应的这个分类的广告集合数据List<Content>
     *            001               List<Content>
     *            002               List<Content>
     *
     *
     */
    @Override
    public List<Content> findByCategoryIdFromRedis(Long categoryId) {
        //1. 首先根据分类id到redis中获取数据
        List<Content> contentList = (List<Content>)redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).get(categoryId);
        //2. 如果redis中没有数据则到数据库中获取数据
        if (contentList == null) {
            //3. 如果数据库中获取到数据, 则放入redis中一份
            contentList = findByCategoryId(categoryId);
            redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).put(categoryId, contentList);
        }


        return contentList;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值