Data Cache solution and cache Solution

Source: Internet
Author: User

Data Cache solution and cache Solution
Data Cache solution by Wu xueying

Today, I think about a caching solution suitable for my project. I have basically tried it (I used CoreData all the time). 1. coredata: Use MagicalRecord + Mogenerator to create a table and write a lot of parsing code. No need to resolve it. This is troublesome. serialization

[NSKeyedArchiver archiveRootObject: model toFile: path];

[NSKeyedUnarchiver unarchiveObjectWithFile: path];

Good and convenient, but I always feel that the experience is not good. After testing the method time, when the model size is large, the resolution time is basically more than Ms, I gave up using 3. sqllite uses an open-source key-value library recommended by Tang qiao in the early stage, which is easy to use.

[[WTUtilsshared]. dbclearTable: @ "RestList"];

[[WTUtilsshared]. dbputObject: responsewithId: @ "restList" Partition Table: @ "RestList"];

Combined with today's Mantle

- (void)loadSavedData {    dispatch_async(dispatch_get_global_queue(0, 0), ^{        id data = [[WTUtils shared].db getObjectById:@"restList" fromTable:@"RestList"];        restListArray = [WTRestListEntity getRestListArray:data];        if (restListArray != nil) {            DELAYEXECUTE(0.0, [_tableView reloadData]);        }else {            [ZAActivityBar showWithStatus:@"loading..."];        }    });}
Finally, the cache effect is good, the Code operation is simple, and the operability is good.




What is the cache in the network?

The Cache Memory is the temporary Memory between the CPU and Memory. It has a smaller capacity than the Memory but is faster than the Memory. The data in the cache is a small part of the memory, but this small part is about to be accessed by the CPU in a short time. When the CPU calls a large amount of data, you can avoid calling the memory directly from the cache to speed up reading. It can be seen that adding cache to the CPU is an efficient solution, so that the entire internal memory (Cache + Memory) becomes a high speed of the existing cache, there is a large memory storage system. The cache has a great impact on CPU performance, mainly because of the Data Exchange sequence of the CPU and the bandwidth between the CPU and the cache.

Cache is used to solve the speed difference between CPU speed and memory speed. The data and commands that are frequently accessed by the CPU in the memory are copied into the cache of the CPU, so that the CPU can not frequently retrieve data in the memory that is as slow as the "Snail, the CPU only needs to be retrieved from the cache, And the cache speed is much faster than the memory speed.

It should be noted that:
1. because the cache is only a replica of a small amount of data in the memory, when the CPU finds data in the cache, it will also fail to be found (because the data is not copied from the memory to the cache ), at this time, the CPU will still go to the memory to find data, so that the system's speed will slow down, but the CPU will copy the data to the cache so that the next time it will not be retrieved from the memory.

2. as time changes, the data that is most frequently accessed is not static. That is to say, the data that is not frequently accessed just now needs to be frequently accessed, the most frequently accessed data is not frequently. Therefore, the cached data must be frequently replaced by certain algorithms to ensure that the cached data is accessed most frequently.

How cache works
[Edit this section]
The principle of caching is that when the CPU needs to read a piece of data, it first looks for it from the cache. if it finds it, it immediately reads it and sends it to the CPU for processing. If it does not, it reads data from the memory at a relatively slow speed and sends it to the CPU for processing. At the same time, it transfers the data block of the data to the cache, this allows you to read the entire block of data from the cache without calling the memory.

This reading mechanism makes the CPU read cache hit rate very high (most CPUs can reach about 90%), that is, 90% of the data to be read by the CPU next time is in the cache, only about 10% needs to be read from memory. This greatly saves the time for the CPU to directly read the memory, and does not need to wait for the CPU to read data. In general, the CPU reads data in the first cache and then the memory.

Level 1 cache and level 2 Cache
[Edit this section]
To distinguish these two concepts, let's first take a look at RAM. RAM is opposite to ROM. After a RAM instance loses power, its information disappears, and the ROM will not disappear after power loss.

There are two types of RAM: static RAM and SRAM; Dynamic RAM and DRAM. The storage speed of the former is much faster than that of the latter, and the memory we use is generally dynamic RAM.

Some cainiao just said: to increase the speed of the system, will it be enough to expand the cache? The larger the expansion, the more data cached, the faster the system will be? The cache is usually static RAM, and the speed is very fast, but the static RAM integration is low (the size of static RAM is 6 times that of dynamic RAM for storing the same data ), the price is high (static RAM with the same capacity is four times that of dynamic RAM). Therefore, it is silly to expand static RAM as a cache, but to improve system performance and speed, we must expand the cache so that we have a compromise. Instead of expanding the original static RAM cache, we need to add some high-speed dynamic RAM as the cache, these high-speed dynamic RAM is faster than conventional dynamic RAM, but slower than the original static RAM cache. We call the original static ram cache a level-1 cache, call the added dynamic RAM as a level-2 cache.

The content in the first-level cache and second-level cache is copies (ing) of frequently accessed data in the memory. They exist to reduce high-speed CPU access to slow memory. Generally, the CPU can find data or commands in the order of first-level cache, then second-level cache. If no data is found, it can only be found in ...... remaining full text>

What method is used to implement the cache mechanism of hibernate?

The cache is between applications and physical data sources. It is used to reduce the frequency of applications accessing physical data sources and improve the running performance of applications. Data in the cache is the replication of data in the physical data source. during runtime, the application reads and writes data from the cache. Data in the cache and the physical data source will be synchronized at a specific time or event.
The cache medium is generally memory, so the read/write speed is very fast. However, if the volume of data stored in the cache is very large, the hard disk will also be used as the cache medium. The implementation of cache should not only consider the storage media, but also take into account the management of concurrent access to the cache and the lifecycle of the cache data.

Hibernate cache includes Session cache and SessionFactory cache. SessionFactory cache can be divided into two types: built-in cache and external cache. The Session cache is built-in and cannot be detached. It is also called the first-level cache of Hibernate. The built-in cache of SessionFactory is similar to the Session cache implementation method. The former is the data contained in some collection attributes of the SessionFactory object, and the latter is the data contained in some collection attributes of the Session. The built-in cache of SessionFactory stores the ing metadata and predefined SQL statements. The ing metadata is a copy of the data in the ing file, the pre-defined SQL statement is derived from the ing metadata in the Hibernate initialization phase. The built-in cache of SessionFactory is read-only, and the application cannot modify the ing metadata in the cache and the pre-defined SQL statement, therefore, SessionFactory does not need to synchronize built-in cache and ing files. The external cache of SessionFactory is a configurable plug-in. By default, SessionFactory does not enable this plug-in. External cache data is a copy of database data, and external cache media can be memory or hard disk. The external cache of SessionFactory is also called the second-level cache of Hibernate.

The two levels of Hibernate cache are located at the persistence layer, which stores copies of database data. What are the differences between them? To understand the differences between the two, we need to have a deep understanding of the two features of the persistence layer cache: the cache scope and the cache's concurrent access policies.

Cache range of The Persistence Layer

The cache scope determines the cache lifecycle and who can access it. The cache scope is divided into three types.

1. Transaction scope: the cache can only be accessed by the current transaction. The cache lifecycle depends on the transaction lifecycle. When the transaction ends, the cache ends the lifecycle. In this range, the cache medium is memory. Transactions can be database transactions or application transactions. Each transaction has its own cache. Data in the cache is usually in the form of correlated objects.

2. Process scope: the cache is shared by all transactions in the process. These transactions may access the cache concurrently, so you must adopt the necessary transaction isolation mechanism for the cache. The cache lifecycle depends on the process lifecycle. When a process ends, the cache ends. The cache within the process scope may store a large amount of data, so the storage media can be memory or hard disk. The data in the cache can be in the form of correlated objects or loose data of objects. The loose object data format is somewhat similar to the object serialization data, but the loose object decomposition algorithm requires faster than the object serialization algorithm.

3. Cluster scope: in a cluster environment, the cache is shared by processes of one or more machines. Data in the cache is replicated to every process node in the cluster environment. Data Consistency in the cache is ensured through remote communication between processes. Data in the cache is usually in the form of loose data of objects.

For most applications, you should carefully consider whether to use the cluster-range cache, because the access speed may not necessarily be much faster than the direct access to database data.

The persistence layer can provide cache in Multiple scopes. If no data is found in the cache of the transaction scope, you can also query it in the cache of the Process scope or cluster scope. If no data is found, you can only query it in the database. The cache of the transaction scope is the first level of the persistence layer, which is usually required... the rest of the full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.