[JAVA] I tried using the cache function of Application Container Cloud Service

Oracle Application Container Cloud Service provides caching functionality. You can use this to cluster applications, substitute database access, and more.

Description

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

function

--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

Premise

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>

how to use

The usage is explained below using the session retention method using HttpSession and Cache as an example.

For HttpSession

If you want to use HttpSession to hold the session, you typically write the code as follows:

Get session object and set data

HttpSession session = request.getSession(true);
session.setAttribute("KEY", "1");

Get session data

String value = (String) session.getAttribute("KEY");

For Cache (com.oracle.cloud.cache.basic.Cache)

1. Initialization

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.

You can choose from two ways to access the cache:

Use ** Transport ** to configure the settings when creating a Session object as shown below:

2. Cache operation

Add / reference / update / delete data to cache as follows

Application Cache environment creation and application execution example

1. Creating a cash service

1.1. Application Cache Selection

Select ** Application Cache ** from the menu.

1.2. Creating a cash service instance

Click ** Create Instance **.

1.3. Cache configuration and capacity settings

--Basic: ** Only one ** A container for the cache will be created. --Recommended: ** 3 or more ** cache containers will be created.

1.4. Confirmation screen

1.5. Creating screen

1.6. Cash service creation completed

2. Deploy the sample application

2.1. Application selection

Select ** Application ** from the menu.

2.2. Deploy Application

Click ** Create Application **.

2.3. Runtime selection

Select ** Java SE **.

2.4. Selecting the application to deploy

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 **.

3. Operation using HttpSession

3.1. Access the sample application

Click the URL displayed on the dashboard screen. Applications accessed from this URL will use HttpSession to count up the number of accesses.

3.2. First access

3.3. Second access

3.4. Third access

3.5. Application restart

Click ** Restart ** from the menu.

3.6. Fourth access after restart

The session data is not persistent, so a reboot initializes the session. Therefore, it will be displayed again as ** First Access **.

4. Operation using Cache

4.1. First access

Access the context root as ** cache **.

-/ session: Servlet with HttpSession behavior -/ cache: Servlet with Cache operation

4.2. Second access

4.3. Third access

4.4. Application restart

4.5. Fourth access after restart

Since the access count information is stored as a cache on the cache service, it continues to count up even after restarting.

5. Clustering with cache

5.1. Deploy the same application with a different name

Rename and deploy the same application. Select the cash service as in the previous step.

5.2. Access the sample application

Access the application that runs the cache.

5.3. Confirmation of clustering operation using cache

We will share the number of accesses of each application and count up each.

GitHub https://github.com/shinyay/oracle-accs-cache-api

Summary

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

I tried using the cache function of Application Container Cloud Service
I tried to develop the cache function of Application Container Cloud Service in the local environment
I tried using the Server Push function of Servlet 4.0
I tried using the profiler of IntelliJ IDEA
I tried using the Migration Toolkit for Application Binaries
[Swift] I tried to implement the function of the vending machine
[Rails] I tried deleting the application
I tried using Pari gp container
I tried using GoogleHttpClient of Java
I tried to touch the asset management application using the emulator of the distributed ledger Scalar DLT
I tried lightly the result cache of the method of JCache (Ehcache 3) feat. Guice
I tried using Docker for the first time
[API] I tried using the zip code search API
I tried using Alibaba Cloud's KMS (Key Management Service) service
I tried using Gson
I tried to summarize the state transition of docker
I tried using Google Cloud Vision API in Java
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried the new feature profiler of IntelliJ IDEA 2019.2.
I tried using TestNG
I tried using Galasa
[WIP] I tried the configuration of Docker + Streama + NFS
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
Limit the number of threads using Java's Executor Service
I tried running a DB access application on IKS + Db2 on IBM Cloud (6. Preparation of DB access application (java))
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I tried to implement the like function by asynchronous communication
I tried using the GitHub repository as a library server
[Rails] I tried using the button_to method for the first time
Guide: Deploy your application using the Alibaba Cloud Toolkit plugin
I tried to summarize the basics of kotlin and java
I tried JAX-RS and made a note of the procedure
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
[Rails] I tried to implement "Like function" using rails and js
I tried using the CameraX library with Android Java Fragment
The story of dieting the container of the Elixir application as much as possible
I tried using azure cloud-init
I tried the Docker tutorial!
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried using Java REPL
I tried the FizzBuzz problem
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to solve the problem of "multi-stage selection" with Ruby
Using the face detection function of Watson Visual Recognition, I tried to process a whole body image of a person into an image of only the face part
[Metal] I tried to figure out the flow until rendering using Metal
I tried to implement Ajax processing of like function in Rails
Deploy your application to EDAS using the Cloud Toolkit Maven plugin
I made a Ruby container image and moved the Lambda function
I tried to implement the image preview function with Rails / jQuery
I tried the input / output type of Java Lambda ~ Map edition ~
Try using the two-point measurement function of Firebase Performance Monitoring. [Android]
I took a look at the resources of Azure Container Instance
I tried to build a simple application using Dockder + Rails Scaffold
I tried to check the operation of gRPC server with grpcurl
I tried to summarize the methods of Java String and StringBuilder
I tried to display the calendar on the Eclipse console using Java.