Skip to content

Commit f12c05b

Browse files
authored
Merge pull request #128 from eclipse/memcached
Creates driver to memcached
2 parents c0bb02e + 3ba0123 commit f12c05b

15 files changed

+845
-0
lines changed

memcached-driver/README.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
image::https://davidmles.com/wp-content/uploads/2016/01/memcached-logo-768x432.png[Memcached Project,align="center"]
2+
3+
4+
**Memcached** (pronunciation: mem-cashed, mem-cash-dee) is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached is free and open-source software, licensed under the Revised BSD license. Memcached runs on Unix-like operating systems (at least Linux and OS X) and on Microsoft Windows. It depends on the libevent library.
5+
Memcached's APIs provide a very large hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.
6+
Memcached has no internal mechanism to track misses which may happen. However, some third party utilities provide this functionality.
7+
8+
9+
10+
11+
=== How To Install
12+
13+
1. Execute the maven install `mvn clean install`
14+
15+
16+
=== Install without testing
17+
18+
19+
If you won't run the tests the database is not required, so just run the maven skipping the tests.
20+
21+
1. Execute the test `mvn clean install -DskipTests`

memcached-driver/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2017 Otávio Santana and others
4+
~ All rights reserved. This program and the accompanying materials
5+
~ are made available under the terms of the Eclipse Public License v1.0
6+
~ and Apache License v2.0 which accompanies this distribution.
7+
~ The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
8+
~ and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
9+
~
10+
~ You may elect to redistribute this code under either of these licenses.
11+
~
12+
~ Contributors:
13+
~
14+
~ Otavio Santana
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>org.jnosql.diana</groupId>
22+
<artifactId>diana-driver</artifactId>
23+
<version>0.0.9-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>memcached-driver</artifactId>
27+
<name>${project.groupId}:${project.artifactId}</name>
28+
<description>The Eclipse JNoSQL communication layer, Diana, implementation to Memcached</description>
29+
<url>http://jnosql.org/</url>
30+
31+
<licenses>
32+
<license>
33+
<name>The Apache Software License, Version 2.0</name>
34+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
35+
</license>
36+
<license>
37+
<name>The Eclipse Public License v1.0</name>
38+
<url>http://www.eclipse.org/legal/epl-v10.html</url>
39+
</license>
40+
</licenses>
41+
42+
43+
<scm>
44+
<connection>scm:git:git://github.com/eclipse/jnosql-diana-driver.git</connection>
45+
<developerConnection>scm:git:ssh://github.com:eclipse/jnosql-diana-driver.git</developerConnection>
46+
<url>https://github.com/eclipse/jnosql-diana-driver</url>
47+
</scm>
48+
49+
<developers>
50+
<developer>
51+
<name>JNoSQL Developers</name>
52+
<email>[email protected]</email>
53+
<organization>Eclipse JNoSQL</organization>
54+
<organizationUrl>https://dev.eclipse.org/mailman/listinfo/jnosql-dev</organizationUrl>
55+
</developer>
56+
</developers>
57+
58+
<dependencies>
59+
<dependency>
60+
<groupId>org.jnosql.diana</groupId>
61+
<artifactId>diana-key-value</artifactId>
62+
<version>${project.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.jnosql.diana</groupId>
66+
<artifactId>diana-driver-commons</artifactId>
67+
<version>${project.version}</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>net.spy</groupId>
71+
<artifactId>spymemcached</artifactId>
72+
<version>2.12.3</version>
73+
</dependency>
74+
</dependencies>
75+
</project>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.memcached.key;
16+
17+
import net.spy.memcached.MemcachedClient;
18+
import org.jnosql.diana.api.Value;
19+
import org.jnosql.diana.api.key.BucketManager;
20+
import org.jnosql.diana.api.key.KeyValueEntity;
21+
22+
import java.time.Duration;
23+
import java.util.Optional;
24+
import java.util.stream.Collectors;
25+
26+
import static java.util.Objects.requireNonNull;
27+
import static java.util.Optional.ofNullable;
28+
import static java.util.stream.StreamSupport.stream;
29+
30+
final class MemcachedBucketManager implements BucketManager {
31+
32+
private static final int NO_EXP = 0;
33+
private final MemcachedClient client;
34+
private final String bucketName;
35+
36+
MemcachedBucketManager(MemcachedClient client, String bucketName) {
37+
this.client = client;
38+
this.bucketName = bucketName;
39+
}
40+
41+
42+
@Override
43+
public <K> void put(KeyValueEntity<K> entity) {
44+
requireNonNull(entity, "entity is required");
45+
put(entity.getKey(), entity.get());
46+
}
47+
48+
49+
@Override
50+
public <K, V> void put(K key, V value) {
51+
requireNonNull(key, "key is required");
52+
requireNonNull(value, "value is required");
53+
set(key, value, NO_EXP);
54+
55+
}
56+
57+
@Override
58+
public <K> void put(KeyValueEntity<K> entity, Duration ttl) {
59+
requireNonNull(entity, "entity is required");
60+
requireNonNull(ttl, "ttl is required");
61+
set(entity.getKey(), entity.get(), (int) ttl.getSeconds());
62+
}
63+
64+
@Override
65+
public <K> void put(Iterable<KeyValueEntity<K>> entities) {
66+
requireNonNull(entities, "entities is required");
67+
entities.forEach(this::put);
68+
}
69+
70+
@Override
71+
public <K> void put(Iterable<KeyValueEntity<K>> entities, Duration ttl) {
72+
requireNonNull(entities, "entities is required");
73+
requireNonNull(ttl, "ttl is required");
74+
entities.forEach(e -> this.put(e, ttl));
75+
}
76+
77+
@Override
78+
public <K> Optional<Value> get(K key) {
79+
requireNonNull(key, "key is required");
80+
return ofNullable(client.get(getKey(key))).map(Value::of);
81+
}
82+
83+
@Override
84+
public <K> Iterable<Value> get(Iterable<K> keys) {
85+
requireNonNull(keys, "keys is required");
86+
87+
return stream(keys.spliterator(), false)
88+
.map(this::get)
89+
.filter(o -> o.isPresent())
90+
.map(Optional::get)
91+
.collect(Collectors.toList());
92+
}
93+
94+
@Override
95+
public <K> void remove(K key) {
96+
requireNonNull(key, "key is required");
97+
client.delete(getKey(key));
98+
}
99+
100+
@Override
101+
public <K> void remove(Iterable<K> keys) {
102+
requireNonNull(keys, "keys is required");
103+
stream(keys.spliterator(), false).forEach(this::remove);
104+
}
105+
106+
@Override
107+
public void close() {
108+
}
109+
110+
111+
private <K> String getKey(K key) {
112+
return bucketName + ':' + key.toString();
113+
}
114+
115+
private void set(Object key, Object value, int exp) {
116+
client.set(getKey(key), exp, value);
117+
}
118+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.memcached.key;
16+
17+
import net.spy.memcached.ConnectionFactory;
18+
import net.spy.memcached.MemcachedClient;
19+
import org.jnosql.diana.api.key.BucketManagerFactory;
20+
21+
import java.io.IOException;
22+
import java.net.InetSocketAddress;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Objects;
26+
import java.util.Queue;
27+
import java.util.Set;
28+
29+
final class MemcachedBucketManagerFactory implements BucketManagerFactory<MemcachedBucketManager> {
30+
31+
private final ConnectionFactory factory;
32+
33+
private final List<InetSocketAddress> addresses;
34+
35+
MemcachedBucketManagerFactory(ConnectionFactory factory, List<InetSocketAddress> addresses) {
36+
this.factory = factory;
37+
this.addresses = addresses;
38+
}
39+
40+
@Override
41+
public MemcachedBucketManager getBucketManager(String bucketName) {
42+
Objects.requireNonNull(bucketName, "bucketName is required");
43+
try {
44+
MemcachedClient client = new MemcachedClient(factory, addresses);
45+
return new MemcachedBucketManager(client, bucketName);
46+
} catch (IOException e) {
47+
throw new MemcachedException("There is an error when try to create da BucketManager", e);
48+
}
49+
}
50+
51+
@Override
52+
public void close() {
53+
}
54+
55+
@Override
56+
public Map getMap(String bucketName, Class keyValue, Class valueValue) {
57+
throw new UnsupportedOperationException("This method is not supported");
58+
}
59+
60+
@Override
61+
public Queue getQueue(String bucketName, Class clazz) {
62+
throw new UnsupportedOperationException("This method is not supported");
63+
}
64+
65+
@Override
66+
public Set getSet(String bucketName, Class clazz) {
67+
throw new UnsupportedOperationException("This method is not supported");
68+
}
69+
70+
@Override
71+
public List getList(String bucketName, Class clazz) {
72+
throw new UnsupportedOperationException("This method is not supported");
73+
}
74+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2019 Otávio Santana and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.jnosql.diana.memcached.key;
16+
17+
import org.jnosql.diana.api.JNoSQLException;
18+
19+
class MemcachedException extends JNoSQLException {
20+
21+
MemcachedException(String message, Throwable cause) {
22+
super(message, cause);
23+
}
24+
}

0 commit comments

Comments
 (0)