当前位置:网站首页>How to design a highly available and extended image storage function

How to design a highly available and extended image storage function

2022-06-24 08:10:00 A more dreams a

The article brief introduction

In this paper, a small e-commerce system is designed Picture storage Module analysis and summary , Share how to design a suitable image storage function .

Common image storage methods

In daily system design , It is inevitable that the picture function will be involved , For example, pictures of commodities 、 Article cover 、 User profile, etc . Create a picture field for the data in the normal way , This field stores the path of the file . As the format below :

Data number

Other fields

Picture field

1

...

2

...

There are good and bad points in the above data table design :

advantage :

  1. Simple storage , Users only need to upload pictures , Get a picture of url Just store it .
  2. Simple presentation , The client only needs to be based on the value of the field , Show it .

Inferiority :

  1. Weak expansibility , If we change the domain name of our pictures later , You need to replace the domain name in all the data .
  2. Weak multi picture scalability , If one piece of data has more than one picture , The stored value may be in this format . Picture path 1, Picture path 2,...., Picture path n. Although storage is simple , However, data format conversion is required in the presentation stage , You need to convert a string to an array format for circular processing .
  3. Waste storage resources , Suppose there is a picture a, I uploaded the user's Avatar once , I also uploaded the product pictures once , In this way, the same picture will be stored twice , Double the storage space .
  4. The first 2 Point refers to the path problem , You may think that I don't store specific domain name information in my picture , Store only the name of the picture , You can splice it when the client shows it .$domain = 'https://www.baidu.com/'; $imageArray = ['1.png', '2.png', '3.png', '4.png']; foreach ($imageArray as $value) { $domain .= $value; } The above method can avoid the domain name problem , But it is also inevitable to avoid the problem of storage mode . It is possible that qiniu cloud storage was used in the early stage of the system , The following applies to Alibaba cloud storage , Tencent cloud storage , This needs to deal with storage methods . Although it can also solve , But it makes the system design more complicated .

Optimization idea

According to the above question , We can think about it , If all the pictures of the system , All of them are made into a functional module , There's not a place that needs to involve pictures , To call the list data in this module , Get the picture of the system , Would this be better ?

Snipaste_2021-06-26_19-37-06

Pictured above , Picture management function of wechat official account . On the left is the classification of pictures , On the right is the corresponding specific picture . Where we need to apply pictures , Go to get the corresponding picture data .

Snipaste_2021-06-26_19-38-47

meanwhile , We also have an independent material library management , You can view all the pictures of the system , Easy to manage pictures .

The project design

The optimization idea mentioned , Let's create a separate image manager . How should we design the data sheet ? Here we will demonstrate the image management of wechat official account .

  1. First, we have a classification of pictures , At this time, you need a picture classification table (image_category).
  2. Next , We need to create a specific picture table (image), Used to store specific picture data .
Snipaste_2021-06-26_19-43-51
  1. Image classification is mainly the name of classification 、 Sort 、 Creation time and other information .
Snipaste_2021-06-26_19-44-01
  1. The picture information table stores more fields . With picture classification id、 Store domain name 、 The name of the picture 、 The image url、 Size of picture 、 Information such as the type of picture .
  2. Take a field in the table to store the domain name , When the client obtains the domain name and the name of the picture , Direct splicing is the completion path of an image .
  3. The size of the picture and the extension of the picture , It is convenient for us to display on the page later . Some systems may show the size of the image .

Summary of the plan

Through the design above , We can summarize the following advantages :

  1. Optimized image storage space , Avoid uploading the same picture multiple times , Occupy the storage space of the system .
  2. If different storage platforms are involved , We have a domain name field in the picture , Just splice the domain name and picture name . In the later stage, no matter how many storage platforms are changed , The system will not have any impact .
  3. It is convenient for the system to manage pictures , In this way, the pictures are stored independently , It is convenient to view all picture data of the system .
  4. Tables involving business data , You only need to associate the id that will do .
原网站

版权声明
本文为[A more dreams a]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210628105202631p.html