1. 今日大纲

  1. 学习Redis
  2. 使用Redis完成项目中缓存需求
  3. 实现商品详情页功能
    1. 缓存的需求

大广告位数据无需每次查询后台系统的接口,可以在前台系统添加缓存,提高访问首页的速度。

商品类目的数据也可以缓存起来。

实现:

使用Redis实现缓存。

目前缓存的主流技术:

  1. Redis
  2. Memcached

二者谁的性能更高?

  1. 单纯从缓存命中的角度来说,是Memcached要高,Redis和Memcache的差距不大
  2. 但是,Redis提供的功能更加的强大

二者的区别是什么?

  1. Memcache是多线程
  2. Redis是单线程
    1. Redis

      1. NoSQL

  1. 主流的NoSQL产品

  1. Redis简介

Redis官网: http://redis.io/

  1. 历史与发展

  1. Redis的特性

  1. Redis版本说明

  1. 下载Redis

Linux版本 2.8.11 :

http://download.redis.io/releases/redis-2.8.11.tar.gz

Windows(64位)版本 2.8.9 :

https://github.com/MSOpenTech/redis/blob/2.8/bin/release/redis-2.8.9.zip?raw=true

Windows(32位)版本 2.6 :

https://github.com/MSOpenTech/redis/blob/2.6/bin/release/redisbin.zip?raw=true

  1. Redis的安装

    1. 安装文件

  1. 安装方式一

双机打开redis-server.exe即可:

测试:

  1. 安装方式二(安装到系统服务)

先删除原有的系统服务:

安装服务:

  1. 32位操作系统安装

只能通过双击打开redis-server.exe启动,不能安装到系统服务。

  1. 注意事项

由于文件系统非NTFS,导致Redis启动失败:

限制Redis的最大内存:

  1. Redis-cli使用

    1. redis-cli的使用之发送命令

  1. redis-cli的使用之命令返回值

  1. Redis的多数据库

FLUSHALL -- 清空所有数据库的所有数据

FLUSHDB -- 清空当前所在数据库的数据

  1. 配置数据库数量

  1. Redis的基本命令

    1. KEYS

  1. EXISTS

  1. DEL

  1. TYPE

  1. HELP

HELP 空格 tab键

  1. Redis的字符串数据类型

    1. 字符串类型

  1. GET、SET

  1. INCR

  1. INCRBY

  1. DECR、DECRBY

  1. APPEND

  1. STRLEN

  1. MSET、MGET

  1. Redis的Hash数据结构

    1. 数据结构

  1. 基本操作

  1. 判断是否存在和选择性插入

  1. Hash的自增长

  1. 只获取字段名或字段值

  1. 获取字段数量

  1. Redis之生存时间

    1. 设置生存时间

TTL返回值:

大于0的数字:剩余生存时间,单位为秒

-1 : 没有生存时间,永久存储

-2 : 数据已经被删除

  1. 清除生存时间

  1. 设置单位为毫秒

  1. 客户端

    1. 支持的语言

  1. Jedis

官网:

  1. Jedis的使用

    1. 导入itcast-redis

  1. 导入依赖

  1. 简单示例

  1. 连接池使用

  1. 分片式集群

存在的问题:无法动态增加减少服务节点。

  1. 分片式集群的使用

public
class ShardedJedisPoolDemo {

public
static
void main(String[] args) {

// 构建连接池配置信息

JedisPoolConfig poolConfig = new JedisPoolConfig();

// 设置最大连接数

poolConfig.setMaxTotal(50);

// 定义集群信息

List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();

shards.add(new JedisShardInfo("127.0.0.1", 6379));

shards.add(new JedisShardInfo("192.168.29.112", 6379));

// 定义集群连接池

ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, shards);

ShardedJedis shardedJedis = null;

try {

// 从连接池中获取到jedis分片对象

shardedJedis = shardedJedisPool.getResource();

// for (int i = 0; i < 100; i++) {

// shardedJedis.set("key_" + i, "value_" + i);

// }

System.out.println(shardedJedis.get("key_49"));

System.out.println(shardedJedis.get("key_7"));

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null != shardedJedis) {

// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态

shardedJedis.close();

}

}

// 关闭连接池

shardedJedisPool.close();

}

}

  1. 将Jedis集成到项目中

    1. 添加缓存前的测试

  1. 后台系统中添加缓存

    1. 在Service中添加依赖

  1. Spring和Jedis的整合

  1. 封装RedisService

package com.taotao.manage.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;

import redis.clients.jedis.ShardedJedisPool;

@Service

public
class RedisService {

@Autowired

private ShardedJedisPool shardedJedisPool;

/**

* 执行set操作

*

* @param key

* @param value

* @return

*/

public String set(String key, String value) {

ShardedJedis shardedJedis = null;

try {

// 从连接池中获取到jedis分片对象

shardedJedis = shardedJedisPool.getResource();

return
shardedJedis.set(key, value);

} finally {

if (null != shardedJedis) {

// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态

shardedJedis.close();

}

}

}

/**

* 执行get操作

*

* @param key

* @return

*/

public String get(String key) {

ShardedJedis shardedJedis = null;

try {

// 从连接池中获取到jedis分片对象

shardedJedis = shardedJedisPool.getResource();

return
shardedJedis.get(key);

} finally {

if (null != shardedJedis) {

// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态

shardedJedis.close();

}

}

}

}

  1. 优化RedisService

定义接口:

RedisService的实现:

package com.taotao.manage.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;

import redis.clients.jedis.ShardedJedisPool;

@Service

public
class RedisService {

@Autowired

private ShardedJedisPool shardedJedisPool;

private <T> T execute(Function<T, ShardedJedis> fun) {

ShardedJedis shardedJedis = null;

try {

// 从连接池中获取到jedis分片对象

shardedJedis = shardedJedisPool.getResource();

return
fun.callback(shardedJedis);

} finally {

if (null != shardedJedis) {

// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态

shardedJedis.close();

}

}

}

/**

* 执行set操作

*

* @param key

* @param value

* @return

*/

public String set(final String key, final String value) {

return
this.execute(new Function<String, ShardedJedis>() {

@Override

public String callback(ShardedJedis e) {

return
e.set(key, value);

}

});

}

/**

* 执行get操作

*

* @param key

* @return

*/

public String get(final String key) {

return
this.execute(new Function<String, ShardedJedis>() {

@Override

public String callback(ShardedJedis e) {

return
e.get(key);

}

});

}

/**

* 执行删除操作

*

* @param key

* @return

*/

public Long del(final String key) {

return
this.execute(new Function<Long, ShardedJedis>() {

@Override

public Long callback(ShardedJedis e) {

return
e.del(key);

}

});

}

/**

* 设置生存时间,单位为:秒

*

* @param key

* @param seconds

* @return

*/

public Long expire(final String key, final Integer seconds) {

return
this.execute(new Function<Long, ShardedJedis>() {

@Override

public Long callback(ShardedJedis e) {

return
e.expire(key, seconds);

}

});

}

/**

* 执行set操作并且设置生存时间,单位为:秒

*

* @param key

* @param value

* @return

*/

public String set(final String key, final String value, final Integer seconds) {

return
this.execute(new Function<String, ShardedJedis>() {

@Override

public String callback(ShardedJedis e) {

String str = e.set(key, value);

e.expire(key, seconds);

return
str;

}

});

}

}

  1. 实现缓存逻辑

  1. 先从缓存中命中,命中则返回
  2. 将结果写入到Redis中
  1. 测试

  1. 原则

原则:缓存逻辑不能影响原有的业务逻辑执行。

  1. 优化之后的缓存实现

  1. 商品详情页

    1. 进入商品详情页地址

http://www.taotao.com/item/{itemId}.html

  1. 商品基本数据显示

    1. Controller

  1. Item对象

  1. Service

  1. 后台系统提供接口服务

  1. Item.jsp中使用模型数据

显示商品价格:

<fmt:formatNumber groupingUsed="false" maxFractionDigits="2" minFractionDigits="2" value="${item.price / 100 }"/>

  1. 测试

  1. 显示商品描述数据

    1. Controller

  1. Service

  1. 后台系统中开放接口服务

  1. Item.jsp中显示数据

  1. 效果

  1. 显示规格参数数据

    1. Controller

  1. Service

  1. Item.jsp

  1. 效果

  1. 字符串拼接面试题

  1. 商品详情页添加缓存

    1. 为什么要添加缓存?

加快显示商品 详情页的速度。

  1. 在哪里添加缓存?

前台系统? 还是 后台系统? --- 都加。

每个团队都应该为自己的系统功能负责任,保证其性能。

  1. 前台系统添加缓存

    1. 导入依赖

  1. 导入Spring和Jedis的整合文件

  1. 将RedisService和Function接口移动至taotao-common

  1. Service中添加缓存逻辑

private
static
final
String
REDIS_KEY = "TAOTAO_WEB_ITEM_DETAIL_";

private
static
final Integer REDIS_TIME = 60 * 60 * 24;

public Item queryItemById(Long itemId) {

// 从缓存中命中

String key = REDIS_KEY + itemId;

try {

String cacheData = this.redisService.get(key);

if (StringUtils.isNotEmpty(cacheData)) {

return
MAPPER.readValue(cacheData, Item.class);

}

} catch (Exception e1) {

e1.printStackTrace();

}

try {

String url = TAOTAO_MANAGE_URL + "/rest/item/" + itemId;

String jsonData = this.apiService.doGet(url);

if (StringUtils.isEmpty(jsonData)) {

return
null;

}

try {

// 将数据写入到缓存中

this.redisService.set(key, jsonData, REDIS_TIME);

} catch (Exception e) {

e.printStackTrace();

}

return
MAPPER.readValue(jsonData, Item.class);

} catch (Exception e) {

e.printStackTrace();

}

return
null;

}

  1. 商品数据同步问题

    1. 问题

后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据。

  1. 解决

后台系统中商品修改后向其他系统发送通知,其他系统做出对应的处理即可。

  1. 怎么通知?

  1. 在前台系统中开发一个接口
  2. 在后台系统中调用该接口
  1. ApiService移动至taotao-common

  1. 实现数据同步存在的问题

通知的实现,代码的耦合度太高了。

怎么解决? -- 消息队列。

淘淘商城_day05_课堂笔记的更多相关文章

  1. 淘淘商城_day11_课堂笔记

    今日大纲 发布前的准备 实施发布 一部分是由我来发布 一部分是由你们来发布 讲解分布式部署架构 测试 功能测试 压力测试 项目实战的准备以及分组 分组 抽取功能 讲解所需要开发的功能 项目部署上线流程 ...

  2. 淘淘商城_day01_课堂笔记

    今日大纲 聊聊电商行业 电商行业发展 11.11 2015双11: 2016年: 预测:2017年的双11交易额将达到:1400亿 电商行业技术特点 淘淘商城简介 淘淘商城的前身 电商行业的概念 B2 ...

  3. 淘淘商城_day04_课堂笔记

    今日大纲 实现首页的大广告位功能 实现内容管理系统 首页的大广告 什么是大广告 JS效果: 点击下面的序号选择查询哪个广告 自动切换 点击图片查询具体的页面 以上是由前端团队来开发. 数据结构 说明: ...

  4. 淘淘商城_day02_课堂笔记

    今日大纲 学习Nginx的使用 实现商品的管理 新增商品 查询商品列表 编辑商品 删除商品 上架和下架商品 学习nginx 开发阶段中的环境 开发环境:自己的电脑 测试环境:提供给测试人员使用的环境 ...

  5. 淘淘商城_day10_课堂笔记

    今日大纲 Dubbo入门学习 使用dubbo优化单点登录系统 系统间服务调用方式 浏览器直接访问 浏览器发起请求,通过ajax或jsonp方式请求: Httpclient方式 系统与系统之间通过Htt ...

  6. 淘淘商城_day09_课堂笔记

    今日大纲 实现购物车 基于Mysql实现读写分离 购物车 需求描述 用户可以在登录状态下将商品添加到购物车 用户可以在未登录状态下将商品添加到购物车 用户可以使用购物车一起结算下单 用户可以查询自己的 ...

  7. 淘淘商城_day08_课堂笔记

    今日大纲 问题,如何实现商品数据的同步? 学习MQ(消息队列) 搭建RabbitMQ的环境 学习RabbitMQ的队列 学习Spring-Rabbit 使用RabbitMQ完成商品数据的同步 如何实现 ...

  8. 淘淘商城_day07_课堂笔记

    今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...

  9. 淘淘商城_day06_课堂笔记

    今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...

随机推荐

  1. 动态SQL语句:定义(一)

    文章系列 动态SQL语句:定义(一) 静态SQL与动态SQL 静态SQL:程序运行前,具有固定的形式和结构的SQL. 动态SQL:程序运行时,能够动态改变形式或结构的SQL. 一些思考和想法 在实际的 ...

  2. jsp-1 简单的应用servlet,并用其跳转页面

    jspweb里面用到的servlet跳转页面的方法 使用的jar包只有 commons-lang3-3.5.jar 运行时,tomcat会先根据web.xml里面的信息,查找servlet <? ...

  3. Flexible 弹性盒子模型之CSS justify-content 属性

    实例 在弹性盒对象的 <div> 元素中的各项周围留有空白: div { display: flex; justify-content: space-around; } 复制 效果预览 浏 ...

  4. Kotlin的扩展函数:扩展Android框架(KAD 08)

    作者:Antonio Leiva 时间:Jan 11, 2017 原文链接:https://antonioleiva.com/extension-functions-kotlin/ 扩展函数是Kotl ...

  5. VirtualBox 主机与虚拟机互通

    文章转载:http://www.cnblogs.com/HD/p/4011323.html 网络要设置才能互通 注意:不启动Linux系统的时候,设置网络 使用VirtualBox的主机与虚拟机相互通 ...

  6. TortoiseSVN 1.8 关于右键的设置

    以前用SVN但都是 IDE 自己集成的插件,最近使用 android studio 发现居然自己不带SVN command line插件,非得自己单独装一个,于是使用了 TortoiseSVN ,但用 ...

  7. 解决mac-osx10.11下无法安装wxPython2.8-osx-unicode-2.8.12.1的问题

    在mac-osx10.11版本下,安装RIDE前提需要装wxPython2.8-osx-unicode-2.8.12.1库,但在安装wxPython过程中,会提示安装失败,以下提供一种解决方案 这里我 ...

  8. Chapter 2 Open Book——32

    I paused for a long moment, and then made the mistake of meeting his gaze. 我停顿了很长时间,然后错误的去对视了他的凝视 我停 ...

  9. css3变形动画

    transform:变形 rotate:旋转 translate:移动 scale:缩放 skew:扭曲 一切变化都是“形变”引起的变化,所以transform就是老大,大家都围着他转 1.trans ...

  10. [ mysql优化一 ] explain解释select语句

    NOSQL  没有什么数据表, 只是一些变量,key_value  ,redis 支持的变量比较多.可以持久化文件到硬盘上. mysql 关系型数据库 ,表和表中间有各种id的关系. 缺点  高并发读 ...