'Implement own in memory cache mechanism in spring boot

I was asked to implement an in memory cache mechanism in my spring boot application. My application is related to social media. User can register and login User can add friend User can post status with privacy criteria public and private User can view public post and private post of their friend post only User can see the post which has more number of likes and they have access to.

How can I implement my own cache mechanism? I should design my own data structure to store cache.

Which api end points can I have cache features?

I'm new to spring.

Thanks in advance



Solution 1:[1]

I done it by creating concurrent hashmap and stored value in runtime and also made that hashmap object Threadsafe. which means only one thread can edit the value of hashmap

Solution 2:[2]

Why not use Caffeine, which is an already built and tested in-memory cache for Java applications? You can find more information at:

It is pretty simple to configure and use. You will save yourself hours of development.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yasar Whizz
Solution 2 João Dias