Detailed explanation of ingenious cache implementation in Android

preface

There are many ways to implement cache. There are many skills and pits. Today, I will introduce some non general methods to help you simply implement some memory cache.

Supplier and memo ize

SQLite is a commonly used data storage method in Android. When accessing database data, you need to use sqliteopenhelper.

A good database connection code should solve the following problems:

a) Build instance comparison cost resources

b) The database connection should be reused

c) Methods such as onupdate cannot conflict with other instances during execution.

It can be simply written like this

This code uses some auxiliary methods provided by guava to implement supplier and memo and logic. As the name suggests, suppliers are generally used as factories, generators, builders and closures. Memoize is similar to the concept of caching. Once an instance is generated, it will return the same instance in subsequent calls. Moreover, it is thread safe.

There are several advantages of writing this way. First, build the instance when necessary, and it will not block the execution of the program at the beginning. Second, it simply implements the cache with memoize to ensure that only one instance is generated.

Code injection

Glow is a heavy user of code injection. It makes our code more structured, clear and simple, and saves a lot of development time.

Dagger 2 is our tool for injection. Interested students should go to the website to learn more about the relevant content. In addition to injection, it also has some complimentary functions, which happen to be used by us to implement caching, and it is very simple. We only need to use a few additional annotations or interfaces.

@Singleton

I believe you should be familiar with this. This is a common question in an interview. In short, it is a singleton. Therefore, with it, you don't have to worry about how to cache these instances.

@Reusable

This is a new cool feature. Although the single instance is very good, sometimes the instance may be too large and can not be recycled. For the time being, the program may not be used, so it feels a little wasteful. In many cases, we don't have such strict requirements. We need a unique instance. If we can reuse it, we can reuse it without re instantiating it. This is the usage scenario of @ reusable. If there is already a generated instance, reuse it instead of re instantiating it. There is no need to guarantee it.

Lazy

Lazy uses something different from the first two@ Singleton and @ reusable are generally used in the definition of services or types, but lazy is used in use. Its use effect is similar to that of supplier and memo mentioned at the beginning.

There will be no sqliteopenhelper instance until you start calling lazysqliteopenhelper get() 。 Once the first instantiation is completed, subsequent calls will return the first result.

Observable

In the process of using app, a lot of data needs to be obtained from the server. In our app, we provide users with some customized content every day, which will not change in the short term. It takes too long to get it from the server every time, but it seems unnecessary to put it in the persistent storage of databases or files. After considering, it seems that memory cache is a good choice.

Therefore, the cache needs to provide the following functions. First, it is a cache. Second, its structure needs to be very simple, because it needs to be used in many places. Third, it has to be thread safe.

Later, our implementation scheme is very simple, using some methods provided by retrofit and observable.

The essence of this method is to use the observable object returned by retrofit, and then observable will provide a cache method similar to cache, so that the network request will not be issued before subscribing, but once there is a result, subsequent callers will get the same result.

be careful

Although the cache is good and easy to use, we must pay attention to data update and thread safety during use, and do not have dirty data.

summary

The above is all the contents of the ingenious cache in Android. I hope the contents of this article can bring some help to your study or work. If you have any questions, you can leave a message for communication.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>