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

How to write a simple cache operation class with Java

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to write a simple cache operation class with Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Analysis

First of all, let's analyze how to design a cache class. Here I implement a cache class in a very simple way, which is the design that I have been using all along.

To clarify the functionality, first define an interface class CacheInt, followed by the utility class CacheUtil that caches the implementation. Then take a look at the features. In order to facilitate access, the cache should be accessed in the form of key-value pairs. In order to adapt to more scenarios, you can add a cache expiration time when accessing, and then add other common methods such as add, get, delete, cache size, whether there is a key, clean up the expired cache, and so on. This is the method of the entire cache tool.

Issues to pay attention to in caching classes:

The cache object should be unique, that is, a singleton

The operation method of cache should be synchronized to prevent errors under the condition of multi-thread concurrency.

Cached containers should have high concurrency performance, and ConcurrentHashMap is a good choice.

2. Specific implementation 1. Definition of CacheInt interface

The definition of CacheInt interface is as follows:

2. The concrete realization of CacheUtil

The core of the cache implementation is CacheUtil, which is explained in combination with comments below. In order to avoid tedious articles, the following screenshots are screenshots of the complete source code, and keep them in order.

The first is the class definition and its attribute definition, in which the instance object of this class is modified by volatile to improve visibility, and the initialization cache capacity is used to initialize the size of the ConcurrentHashMap cache container, which is optimized according to the actual application scenario.

Then there is the definition of the inner class Entry, which is used to store the actual data. In order to facilitate the processing of expiration time, attributes such as initialization timestamp and expiration time are added.

Then use the double check lock singleton method to get the instance object of this class, because the singleton can only have unique characteristics, so note that the constructor needs to be set to private

The next step is to store the cached data put () method. The clearExpiredCache () here is to clean up the expired cache. You will see the method body later. Because there are few cases in which the cache is stored in my project, I fixed to clean the expiration time cache before each storage. Here, you can optimize it according to the actual situation of your project.

Then there is the get cache get () method, because it takes a lot of time to get data, so here I set a 1/3 probability to clean up the expired cache, release heap memory appropriately, and detect whether it expires on acquisition, and delete and return null if it has expired but still got it.

Then there are some more conventional methods, which can be found in the code.

The last method is to clean the expired cache. Here you can choose to start a listening thread to clean the cache in real time, or you can choose to clean it at an appropriate time. For example, in my case, the cache is cleaned at a fixed or probabilistic rate when there are put and get operations.

Third, concurrent testing

Ordinary implementation testing will not be shown here, and there must be no problem. Readers can simply write some test samples. Here, we will mainly show concurrent testing, because there is concurrent processing cache in the actual situation, in order to ensure its correctness, so concurrent testing must be done. I will release my test example below.

In the end, the performance of the test is very good, and there is no incorrect situation. Screenshots of some test results are as follows:

This is the end of "how to write a simple cache operation class with Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report