



Memcached is an easy-to-use, high-performance, in-memory data store. It offers a mature, scalable, open-source solution for delivering sub-millisecond response times, unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. This tutorial will cover how to use Memcached in Jakarta EE with Eclipse JNoSQL.

Installing Memcached Using Docker

To install RavenDB using Docker follows the step below:

Install Docker. Run Docker command:

docker run -d --name memcached-instance -p 11211:11211 memcached:latest





Setting the Application Dependencies

The next step is to configure a smooth Java SE application with CDI and Eclipse JNoSQL with Memcached. Internally, the communication — aka Diana — layer does not create its own communication; it uses the Memcached client and uses the Communication API as an adapter. This demo uses a Maven project that needs to set the dependencies beyond CDI 2.0 implementation. It needs the Eclipse JNosQL dependencies as shown below:

<dependencies> <dependency> <groupId>org.jnosql.artemis</groupId> <artifactId>artemis-key-value</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.jnosql.diana</groupId> <artifactId>memcached-driver</artifactId> <version>${project.version}</version> </dependency> </dependencies>





Equally important is to do a BucketManagerProducer , eligible to CDI setting an instance as a producer.





import org.jnosql.diana.api.key.BucketManager; import org.jnosql.diana.api.key.BucketManagerFactory; import org.jnosql.diana.api.key.KeyValueConfiguration; import org.jnosql.diana.memcached.key.MemcachedKeyValueConfiguration; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; @ApplicationScoped public class BucketManagerProducer { private static final String BUCKET = "developers"; private KeyValueConfiguration<?> configuration; private BucketManagerFactory<?> managerFactory; @PostConstruct public void init() { configuration = new MemcachedKeyValueConfiguration(); managerFactory = configuration.get(); } @Produces public BucketManager getManager() { return managerFactory.getBucketManager(BUCKET); } }





This sample will use a natural model to a user of a system.

import org.jnosql.artemis.Entity; import org.jnosql.artemis.Id; import java.io.Serializable; import java.util.List; import java.util.Objects; @Entity public class User implements Serializable { @Id private String userName; private String name; private List<String> phones; //getter and setter }





The model and the configuration are ready, and it is time to create the class that will run this demo.

import org.jnosql.artemis.key.KeyValueTemplate; import javax.enterprise.inject.se.SeContainer; import javax.enterprise.inject.se.SeContainerInitializer; import java.util.Arrays; import java.util.Optional; public class App { private static final User USER = User.builder(). withPhones(Arrays.asList("234", "432")) .withUsername("username") .withName("Name") .build(); public static void main(String[] args) { try (SeContainer container = SeContainerInitializer.newInstance().initialize()) { KeyValueTemplate template = container.select(KeyValueTemplate.class).get(); User userSaved = template.put(USER); System.out.println("User saved: " + userSaved); Optional<User> user = template.get("username", User.class); System.out.println("Entity found: " + user); } } private App() { } }





To make that integration easier, there is the repository that handles the implementation of the methods that do exist in the Repository interface and new methods once its method query convention.

import org.jnosql.artemis.Repository; public interface UserRepository extends Repository<User, String> { } import org.jnosql.artemis.DatabaseQualifier; import javax.enterprise.inject.se.SeContainer; import javax.enterprise.inject.se.SeContainerInitializer; import java.util.Arrays; import java.util.Optional; public class App2 { private static final User USER = User.builder(). withPhones(Arrays.asList("234", "432")) .withUsername("username") .withName("Name") .build(); public static void main(String[] args) { try (SeContainer container = SeContainerInitializer.newInstance().initialize()) { UserRepository repository = container.select(UserRepository.class, DatabaseQualifier.ofKeyValue()).get(); repository.save(USER); Optional<User> user = repository.findById("username"); System.out.println("User found: " + user); System.out.println("The user found: " + repository.existsById("username")); } } private App2() { } }





This post covered how easy it is to use Memcached.



Memcached code sample.



