Oracle Application Container Cloud Service provides caching functionality. You can use this to cluster applications, substitute database access, and more.
The caching capabilities of Oracle Application Container Cloud Service are easy to use. Creating a cache service is completed by simply setting the capacity used for the cache.
The following two methods are provided to access this cache function.
** REST API ** is a method to directly use the REST API provided by the cash service by using the REST Client API such as Jersey.
** Java API ** is a method to utilize the API for the cache service from within the application by using the library provided by Oracle. The following steps describe this Java API method.
It's an easy-to-use cash service, but it has the following usage rules:
--Multiple caches can be created in one cache service --One cache service can be used simultaneously from an application. (Multiple caches can be used in the cache service) --A cash service can be used by multiple cash services at the same time
--Cache data is replicated within cluster members ――Even if one cache is corrupted, it can be automatically recovered from another cache. --Data is saved even when the cash service is restarted --Scalable configuration
The following libraries are required to use the Java API for the cache service client application.
It can be obtained by describing the following dependency by Maven:
<dependency>
<groupId>com.oracle.cloud.caching</groupId>
<artifactId>cache-client-api</artifactId>
<version>1.0.0</version>
</dependency>
The usage is explained below using the session retention method using HttpSession and Cache as an example.
If you want to use HttpSession to hold the session, you typically write the code as follows:
HttpSession session = request.getSession(true);
session.setAttribute("KEY", "1");
String value = (String) session.getAttribute("KEY");
First, get the cache from the cache service, and if it does not exist, create the cache.
import com.oracle.cloud.cache.basic.Cache;
import com.oracle.cloud.cache.basic.RemoteSessionProvider;
import com.oracle.cloud.cache.basic.Session;
import com.oracle.cloud.cache.basic.options.Transport;
import com.oracle.cloud.cache.basic.options.ValueType;
String CACHE_HOST = System.getenv("CACHING_INTERNAL_CACHE_URL");
String CACHE_URL = "http://" + CACHE_HOST + ":8080/ccs/";
String CACHE_NAME = "sample";
Session cacheSession = new RemoteSessionProvider(CACHE_URL).createSession(Transport.rest());
Cache cache = cacheSession.getCache(CACHE_NAME, ValueType.of(String.class));
The URL of the cache service is obtained from an environment variable. The environment variable used is ** CACHING_INTERNAL_CACHE_URL
**. Use System.getenv ()
to get the value.
String CACHE_HOST = System.getenv("CACHING_INTERNAL_CACHE_URL");
You can choose from two ways to access the cache:
Use ** Transport ** to configure the settings when creating a Session object as shown below:
RemoteSessionProvider(CACHE_URL).createSession(Transport.rest());
Add / reference / update / delete data to cache as follows
cache.put("KEY", "VALUE")
--Referencecache.get("KEY")
--Updatecache.replace("KEY", "VALUE")
cache.remove("KEY")
Select ** Application Cache ** from the menu.
Click ** Create Instance **.
--Basic: ** Only one ** A container for the cache will be created. --Recommended: ** 3 or more ** cache containers will be created.
Select ** Application ** from the menu.
Click ** Create Application **.
Select ** Java SE **.
Select the application you downloaded from the URL below :
In the choices at the bottom of the screen, select the created cash service.
Click ** Create **.
Click the URL displayed on the dashboard screen. Applications accessed from this URL will use HttpSession to count up the number of accesses.
Click ** Restart ** from the menu.
The session data is not persistent, so a reboot initializes the session. Therefore, it will be displayed again as ** First Access **.
Access the context root as ** cache **.
-/ session: Servlet with HttpSession behavior -/ cache: Servlet with Cache operation
Since the access count information is stored as a cache on the cache service, it continues to count up even after restarting.
Rename and deploy the same application. Select the cash service as in the previous step.
Access the application that runs the cache.
We will share the number of accesses of each application and count up each.
GitHub https://github.com/shinyay/oracle-accs-cache-api
Before the introduction of the cache function, the only way to persist data was to store it in a database or as a file, so it is convenient to be able to use the cache function. Also, the cache function did not have a Java API when it first appeared, but I think that it is also convenient in terms of coding because the Java API is also provided.
Recommended Posts