Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Redis caching

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what the principle of Redis caching is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. What is Redis?

Redis is a high-performance open source, C-language written Nosql (non-relational database), with data stored in memory. Redis is stored in key-value form, which is different from traditional relational database. It does not necessarily follow some basic requirements of traditional databases, such as sql standards, transactions, table structure, etc., non-relational database is not strictly a database, but should be a collection of structured data storage methods. Data structure in Java: String, array, list,set map... Redis provides many methods that can be used to access data of various data structures.

two。 Characteristics (advantages)

1. The data is stored in memory with fast access speed and strong concurrency ability.

two。 It supports relatively more value types stored, including string (string), list (linked list), set (collection), zset (sorted set-ordered collection) and hash (hash type).

The emergence of 3.redis makes up for the deficiency of key/value storage like memcached to a great extent, and can play a good complementary role to relational databases (such as MySQL) in some cases.

4. It provides clients such as Java,C/C++,C#,PHP,JavaScript, which is easy to use.

5.Redis supports clustering (master-slave synchronization, load balancing). Data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers.

6. Persistence is supported, and the data can be saved in a file on the hard disk.

7. Support subscription / publish (subscribe/publish) function QQ group

1. Data storage: stored in memory and persisted to disk from time to time. The access speed is fast, the concurrency ability is strong, and the data is not lost after the power outage.

2. More Value types are supported.

3. Multiple clients (language java php c # js)

4. Support clusters to expand spatial 8G+8G+16G

5. Kaiyuan (free and maintained by many people)

3. Install the Redis server

The official download site of Redis is http://redis.io/download. You can go there to download the latest installer.

3.1. Installation and use under windows

1. Download redis program software

Use redisbin32 or redisbin64

two。 Green software, do not need to install, directly use

3. Start the redis service (with and without configuration files)

4. Connect to redis for operation

Cmd > {% redis%} / redis-cli-h ip address-p port number

Ip defaults to local-p defaults to 6379

Redis-cli-h 172.16.6.248-p 6379

Cmd > {% redis%} / redis-cli

Basic usage

2. Redis persistence configuration

Redis provides two different levels of persistence: RDB and AOF, which can be configured by modifying redis.conf.

When the persistence condition is met, it will be persisted, and the data that has not been saved in time will be saved in the form of aof logs.

When Redis starts, parse the log files (a bunch of commands) and recover the data. Then you have to load the rdb file (take and merge).

4.RDB mode

RDB persistence can generate a point-in-time snapshot of a dataset within a specified time interval, which is turned on by default.

How to turn off rdb mode:

Save ""

Save 9001 / / change storage synchronization at least once in a period of 900s

Save xxx save 60 10000

5.AOF log append mode

AOF persistence records all write commands executed by the server and restores the dataset by re-executing these commands when the server starts, turning off the mode by default.

How to turn on aof mode:

Appendonly yes / / yes is on, no is off

# appendfsync always / / every time fsync is executed with a new command, the buffer data is put into the aof file

# here we enable everysec

Appendfsync everysec / / fsync once per second

# appendfsync no / / never fsync (leave it to the operating system to handle, it may take a long time to execute fsync)

For other parameters, please refer to the redis.conf configuration file for details.

6.Redis Classic practical scenario-caching

6.1 Why use caching

Frequently queried data, rarely modified data are stored in the cache, reduce access to the database, reduce database pressure, and the cache is generally memory, access speed is relatively fast.

6.2 which data is suitable for caching

Query frequently: caching is to provide efficient access to data queries.

Rarely modified: modify the cache and database synchronously when you modify

For example: regional data, commodity classification, data dictionary menu (regardless of permissions)

6.3 Select the appropriate cache

Hibernate Tier 2 cache, mybatis Tier 2 cache, redis central cache

Hibernate second-level cache. Mybatis second-level cache does not support cluster cache by default. Use redis.

6.4 how to store data

1) json: convert the data to be stored into a string of type json

When saving the cache:

Java Object- > json string

Get the cache:

Json string-> Java Object-

Json framework: jdk-json-lib jackson gson fastjson

2) binary storage: serialize the data to be stored into a binary serialization framework

7. Implement menu caching

On the principle of Redis caching is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report