Caching(三)|学习笔记

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 快速学习 Caching(三)

开发者学堂课程【高校精品课-上海交通大学-企业级应用体系架构:Caching】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/75/detail/15833


Caching (三)

 

内容介绍

Memcached

MemCached - Storage Commands

、MemCached - Memory Management

Redis

、Clusterina

四、Redis

1、Redis is what is called a key-value store,

often referred to as a NoSQL database.

The essence of a key-value store is the ability to store some data, called a value, inside a key.

2、Redis is an open source, BSD licensed, advanced key-value store.

It is often referred to as a data structure server since keys can

contain strings, hashes, lists, sets and sorted sets.

3、In order to achieve its outstanding performance, Redis works with an in-memory dataset.

Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while, or by appending each command to a log.

4、Redis also supports trivial-to-setup master-slave replication, with very fast non-blocking first synchronization, auto-reconnection on net split and so forth.

memcached 是纯内存,如果崩里面的内存就没用的更多 redisredis 也是 key value 存储只要是key value,就一定是 nosql

是打包存储,不再做更犀利的结构化的设置值比 memcached 复杂一点里面可以包含不同的类型看起像是内存里面的数据集可以把数据写到硬盘上如果 redis 关闭或者崩溃,数据还在从硬盘上读到内存里,再存到硬盘里现在所有的开发不再笔记本上做所以看不出但是实际的开发是数据库在一台机器上机器在内网里tomcat 在另外一台机器上memcached 或者 redis 可能跟 tomcat 在一起跨过网络找服务器上的数据库读进数据写入到 redis 中redis 会写硬盘注意硬盘在本地读本地硬盘的速度一定比读远程硬盘的速度快通过网络访问瓶颈在网络的带宽上,直接读本地硬盘的瓶颈在硬盘的 io 上,io 比网络的 io 大,拿回之后写的是本地的硬盘如果不是这种情况三台机器访问,都网络问题一个非常远的机器做缓存显然不合适,从物理的部署上尽管是三台机器也许在同一个网段里都在1204.40的网段里也许在另外一台网段里比如有很大的系统数据库是集中存放在某一个位置,在软件学院在电信大楼,在不同的大楼里面都摆个应用服务在应用服务器的旁边,在同一个局域网里并排再摆个缓存服务数据库在行政大楼每次在行政大楼读数据之后马上就放到局域网里面靠在一起的 redis 服务器的缓存,也会写硬盘未来 redis 崩了再从硬盘里读数据速度会比到行政大楼重新抓redis 往硬盘上写的意义tomcat 和 redis 跑在一起以及跑在不同的上,靠的数据库服务器要近两种情况

redis 下载好之后安装,windows 和 linux 上差不多只要下载之后在平台上编译

Download, extract and compile Redis with:

$ wget http://download . redis. io/releases/redis-5.0.7.tar.gz

$ tar xzf

redis-5.0.7.tar .gz

$cd redis-5.0.7

$make

Run redis server

$ src/redis-server 运行方法

redis-server. Exe Windows 上的文件可以跑

Run redis Client

$ src/redis-cli 客户端

redis> set foo bar 

OK

redis> get foo 利用命令进行操作

"bar"

最好使用代码访问

Structure type

What it contains

Structure read/ write ability

STRING

Strings, integers, or floating-point values

Operate on the whole string, parts, increment/decrement the integers and floats

LIST

Linked list of strings

Push or pop items from both ends, trim based on offsets, read individual or multiple items, find or remove items by value

SET

Unordered collection of unique strings

Add, fetch, or remove individual items, check

membership, intersect, union, difference, fetch

random items

HASH

Unordered hash table of keys to values

Add, fetch, or remove individual items, fetch the

whole hash

ZSET (sorted set)

Ordered mapping of string

members to floating-point

scores, ordered by score

Add, fetch, or remove individual values, fetch

items based on score ranges or member value

redis 值可以是字符串列表排序的级

STRING in Redis

key 是 hello值是字符串类型world对它可以进行 get set del 操作

Command

What it does

GET

Fetches the data stored at the given key

SET

Sets the value stored at the given key

DEL

Deletes the value stored at the given key (works for all types)

List in Redis

Key 名字list 里面有若干对 item

Command

What it does

RPUSH

Pushes the value onto the right end of the list右段末尾

LRANGE

Fetches a range of values from the list范围

LINDEX

Fetches an item at a given position in the list索引

LPOP

Pops the value from the left end of the list and returns it左端是开口

Set in Redis

Set 跟 list 差不多但是 set 的特点是没有重复元素安全认证有提到过

Command

What it does

SADD

Adds the item to the set

SMEMBERS

Returns the entire set of items

SISMEMBER

Checks if an item is in the set检查所有元素

SREM

Removes the item from the set, if it exists

Sorted set in Redis

hash 需要掌握的是 map相当于 json 的值是另外的 json会有一堆的 key 和 value有获取所有和删除的操作

Command

What it does

HSET

Stores the value at the key in the hash

HGET

Fetches the value at the given hash key

HGETALL

Fetches the entire hash

HDEL

Removes a key from the hash, if it exists

Sorted set in Redis

有排序的一列值竖着类型进行排序达到一定条件进行排序

Command

What it does

ZADD

Adds member with the given score to the ZSET

ZRANGE

Fetches the items in the ZSET from their positions in sorted order

ZRANGEBYSCORE

Fetches items in the ZSET based on a range of scores

ZREM

Removes the item from the ZSET, if it exists

比较 redis 和 memcached 以及关系型数据库非关系型数据库的差异,前两个是内存里的后面是硬盘上关联型数据库非常多查询都有自己的一些语句,查询语句比较多扩展的一些特性和常用的缓存没太大关联查询都有自己的语句关系型数据库是最强大的还有扩展的特性

Name

Type

Data storage options

Query types

Additional fe atures

Redis

In-memory

non-re lational

database

Strings, lists, sets,hashes, sorted sets

Commands for each data

type for common access

patterns, with bulk oper-

ations, and partial trans-

action support

Publish/ Subscribe,

master/ slave replica-

tion, disk persistence,

scripting (stored proce-

dures)

memcached

In-memory

key-value

cache

Mapping of keys to

values

Commands for create,

read, update, delete,

and a few others

Multithreaded server

for additional perfor-

mance

MySQL

Relational

database

Databases of tables

ohrows, views over

tables, spatial and

third-party extensions

SELECT, INSERT,

UPDATE, DELETE,

functions, stored

procedures

ACID compliant (with

InnoDB), master/slave

and master/ master

replication

PostgreSQL

Relational

database

Databases of tables

of rows, views over

tables, spatial and

third-partyextensions,

customizable types

SELECT, INSERT,

UPDATE, DELETE,

built-in functions, cus-

tom stored procedures

ACID compliant, mas-

ter/ slave replication,

multi-master replica-

tion (third party)

MongoDB

On-disk

non-re lational

document

store

Databases of tables

of schema-less BSON

documents

Commands for create,

read, update, delete,

conditional queries,

and more

Supports map-reduce

operations, master/

slave replication, shard-

ing, spatial indexes

RedisConfig.java

image.png

代码和 memcached 特别像产生 configuration使用缓存里面写一堆东西客户端在操作的时候需要得到spring 里面产生 redistemlate可以看到没有特别的地方,产生 template在里面没有额外的非常复杂的设置

image.png

类没有特殊配制跟 redis 和 memcached 类似

PersonDaoImpl.java

image.png

助手类里面获取对象user+id到 redis中如果没取到就调数据库操作获取设置到 redis 中key 就是user+id值就是把person对象转成 json 对象存进去如果不为空从 redis 中能得到 person但是当时存储的时候是存的 json 对象所以要把 json 对象按照 person 里的结构解析出来还原成 person  对象启动之后找曹操第一次找不再 redis 中在数据库里返回放到 redis第二次再找出结果再找会告诉人就在 redis中

启动 redis 服务服务起来之后跑应用,2不在 redis中再刷新一次搜索2这个人在 redis事实证明第一次不再redis中访问之后在 redis 中第二次访问时并没有到数据库里面进行抓取memcached 一样两个例子都在告诉利用缓存把已经从数据库里读取的内容缓存到本地不用 jpa

image.png

不能达到内存的充分利用把缓存专门过滤出来

image.png

memcached 的实现 

既然 redis 做持久化从已经持久化的数据库拿数据有什么区别数据库本身离的比较远开销比较大redis 在一台机器上即便不在一台机上一定也是靠很近的一台机器如果远确实意义不大无论是 memcached 还是redis要注意它们的数据一旦在内存里缓存在写数据库的时候要考虑缓存和数据库的数据要一致简单的方法写数据库,就从缓存里删掉,下次再访问的时候又会到数据库里拿一次,写到 redis 或者是 memcached 的系统复杂的方式在写入数据库的时候,同时写缓两个工具都能实现缓存的目的但是推荐用 redis因为 memcached 更新不如  redis后续更新不知道,memcached 只写内存不做持久化不像 redis 崩了之后还可以从本地的硬盘恢复数据所以建议用 redis


五、Clusterina

1、A large-scale system typically:

Has many user, potentially in many different places

Is long-running, that is, required to be "always up"

Processes large numbers of transactions per second

May see increases in both its user population and system load

Represents considerable business value

Is operated and managed by multiple persons

2、Essential requirements on large-scale systems are often

summarized by the following three properties(RAS):

Reliability

Availability

Serviceability

Scalability

缓存服务器可以建集群应用服务器也可以建集群

相关文章
解决报错:AddressSanitizer: heap-buffer-overflow
leetcode使用AddressSanitizer检查内存是否存在非法访问。报此错,主要是访问了非法内容。 解决方法:数组访问越界,导致此错,后来发现是在访问二维数组的边界row和col弄反了。。
3447 0
|
存储 消息中间件 分布式计算
|
分布式计算 Hadoop
hadoop 的启动和停止命令(史上最全)
sbin/start-all.sh 启动所有的Hadoop守护进程。包括NameNode、 Secondary NameNode、DataNode、ResourceManager、NodeManager sbin/stop-all.sh 停止所有的Hadoop守护进程。包括NameNode、 Secondary NameNode、DataNode、ResourceManager、NodeManager sbin/start-dfs.sh 启动Hadoop HDFS守护进程NameNode、SecondaryNameNode、DataNode sbin/stop-dfs.sh 停止Hadoop
请求分页给出逻辑地址,求对应的物理地址
请求分页内存管理中如何根据逻辑地址计算物理地址的过程,包括确定页号、计算偏移量,并将其转换为物理地址。
331 1
请求分页给出逻辑地址,求对应的物理地址
|
Python
递归魔法:判断字符串是否为回文
本文介绍了如何使用递归判断一个字符串是否是回文。回文字符串是指正读和反读都相同的字符串。文章详细讲解了递归的基本思想和Python实现,并通过多个示例验证了函数的正确性。递归方法通过将大问题分解成更小的子问题,使得判断回文变得简单高效。
354 5
|
12月前
|
监控 算法 Java
深入理解Java虚拟机(JVM)的垃圾回收机制
【10月更文挑战第21天】 本文将带你深入了解Java虚拟机(JVM)的垃圾回收机制,包括它的工作原理、常见的垃圾收集算法以及如何优化JVM垃圾回收性能。通过本文,你将对JVM垃圾回收有一个全新的认识,并学会如何在实际开发中进行有效的调优。
326 0
|
存储 自然语言处理 安全
『软件工程9』结构化系统分析——解决软件“做什么”问题
该文章深入讲解了结构化系统分析方法,用于明确软件需求和解决软件“做什么”的问题,并介绍了在此过程中使用的工具和技术。
『软件工程9』结构化系统分析——解决软件“做什么”问题
|
存储 分布式计算 Hadoop
Hadoop 运行的三种模式
【8月更文挑战第31天】
1193 0
|
存储 NoSQL 关系型数据库
MongoDB中的索引操作总结
这篇文章总结了MongoDB中索引的概念、创建方法、常见操作指令、限制以及索引对查询效率的影响。
773 2
|
SQL Java 数据库连接