Skip to content
This repository was archived by the owner on Aug 13, 2018. It is now read-only.

Commit 22fb2bd

Browse files
committed
Merge branch 'develop' of https://github.com/Fork-World/rapidpm-binarycache into develop
# Conflicts: # README.md # bom/pom.xml # modules/app/cache/provider/hazelcast/src/test/java/junit/org/rapidpm/binarycache/provider/hazelcast/v001/HazelcastCacheImplTest001.java # modules/app/cache/provider/hazelcast/src/test/java/junit/org/rapidpm/binarycache/provider/hazelcast/v002/HazelcastCacheImplTest002.java # modules/client/connect/rest/src/main/java/org/rapidpm/binarycache/client/connect/rest/BinaryCacheRestClient.java # pom.xml
2 parents c887671 + df25358 commit 22fb2bd

File tree

29 files changed

+717
-105
lines changed

29 files changed

+717
-105
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ local.properties
130130
# TeXlipse plugin
131131
.texlipse
132132

133+
/modules/app/cache/provider/jcs/jcs_swap/

README.md

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
1-
# INfos
1+
# BinaryCache
22

3-
## UI Tests with Testbench
4-
Read the documentation on the Vaadin site: [Vaadin Testbench](https://vaadin.com/docs/-/part/testbench/testbench-overview.html)
3+
## Overview
4+
- RapidPM Microservice
5+
- Different JCache implementations, e.g. EHCache or Hazelcast Cache
6+
- Inmemory interface for direct access to the cache functionality
7+
- REST endpoints for access via http
8+
9+
## JCache?
10+
Here's a very good article from Matti Tahvonen telling you more about the JSR 107: [JCache, why and how?](https://vaadin.com/blog/-/blogs/jcache-why-and-how-)
511

6-
Install the Browser dependend driver
7-
* Chrome [https://sites.google.com/a/chromium.org/chromedriver/downloads](https://sites.google.com/a/chromium.org/chromedriver/downloads)
8-
* Firefox
9-
* PhantomJS
12+
## Build your own cache
13+
This project comes with an so called **aggregate** module, enabling the configuration of the cache components
14+
by using Maven profiles. By default, the aggregate will produce a jar containing the ehcache and a corresponding REST endpoint to the cache.
15+
Here's an example how to produce a cache with REST and Hazelcast:
16+
1. Disable the default profile **binarycache-provider-ehcache**
17+
2. Enable the profile **binarycache-proivder-hazelcast**
18+
3. Check that the **binarycache-connect-rest** profile is still active
19+
4. Execute a clean&install for the **aggregate** module
20+
5. You'll find a customized jar cache-hazelcast-rest-xxx.jar in the modules target folder
1021

22+
## Interfaces
23+
Currently there are two different ways to access the cache from a clients perspective.
24+
First, you can use the provided **inmemory** interface. Just add the following dependency to your Maven project.
25+
You'll find an example how to use it in the module **rapidpm-binarycache-modules-client-connect-inmemory**
26+
27+
```xml
28+
<dependency>
29+
<groupId>org.rapidpm.binarycache</groupId>
30+
<artifactId>rapidpm-binarycache-modules-app-cache-api</artifactId>
31+
<version>...</version>
32+
</dependency>
33+
```
34+
35+
Second, you could also use the **REST enpoints**, provided by the BinaryCache implementation.
36+
Using the BinaryCacheRestClient, your calls will be transformed into http request to the cache.
37+
Add the following dependency to use it in your project.
38+
39+
```xml
40+
<dependency>
41+
<groupId>org.rapidpm.binarycache</groupId>
42+
<artifactId>rapidpm-binarycache-modules-client-connect-rest</artifactId>
43+
<version>...</version>
44+
</dependency>
45+
```
46+
47+
## Provider
48+
49+
### Ehcache
50+
From the website [ehcache.org](http://www.ehcache.org/):
51+
*"Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache because it's robust, proven, full-featured, and integrates with other popular libraries and frameworks. Ehcache scales from in-process caching, all the way to mixed in-process/out-of-process deployments with terabyte-sized caches."*
52+
53+
You'll find a default configuration XML in the resource folder config. To use a different configuration file set path to this file with the property **binarcache.config**. [Link]((http://www.ehcache.org/documentation/3.0/xml.html)) to the documentation about configuration files.
54+
55+
56+
### Hazelcast
57+
The implementation of the JSR 107 (JCache) from Hazelcast offers a distribution of the cache content other several instances, based on Hazelcast's own in-mmeory data grid solution. See [Hazelcast Cache](https://hazelcast.com/use-cases/caching/jcache-provider/) for more information.
58+
59+
### Other possible providers
60+
- Infinispan
61+
- Apache Ignite
62+
- JCS

aggregate/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
<artifactId>rapidpm-binarycache-aggregate</artifactId>
1414

15+
<dependencies>
16+
<dependency>
17+
<groupId>org.rapidpm.microservice</groupId>
18+
<artifactId>rapidpm-microservice-modules-core</artifactId>
19+
<version>0.8.1-SNAPSHOT</version>
20+
</dependency>
21+
</dependencies>
22+
1523
<build>
1624
<finalName>cache-${provider}-${connect}-${project.version}</finalName>
1725
<plugins>

bom/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<packaging>pom</packaging>
1515

1616
<properties>
17-
<rapidpm.version>0.9.0</rapidpm.version>
18-
<vaadin.version>8.1.4</vaadin.version>
17+
<rapidpm.version>0.8.1</rapidpm.version>
18+
<vaadin.version>8.0.5</vaadin.version>
1919
<vaadin-testbench-api.version>5.0.0</vaadin-testbench-api.version>
2020
</properties>
2121

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package junit.org.rapidpm.binarycache.api;
2+
3+
import org.junit.Test;
4+
import org.rapidpm.binarycache.api.CacheKey;
5+
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertNull;
8+
9+
/**
10+
* Copyright (C) 2010 RapidPM
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* <p>
21+
* Created by m.lang - RapidPM - Team on 04.05.2017.
22+
*/
23+
public class CacheKeyTest001 {
24+
25+
@Test
26+
public void test001() throws Exception {
27+
assertNotNull(ValidatingKey.getInstance("1_d"));
28+
assertNull(ValidatingKey.getInstance("d_1"));
29+
assertNull(ValidatingKey.getInstance(""));
30+
}
31+
}
32+
33+
class ValidatingKey implements CacheKey {
34+
35+
private String key;
36+
37+
private ValidatingKey(String key) {
38+
this.key = key;
39+
}
40+
41+
public static CacheKey getInstance(String key) {
42+
return key.matches("\\d+_\\w+") ? new ValidatingKey(key) : null;
43+
}
44+
45+
@Override
46+
public String keyAsString() {
47+
return this.key;
48+
}
49+
}
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package junit.org.rapidpm.binarycache.connect.rest.util;
2+
3+
import org.junit.Test;
4+
import org.rapidpm.binarycache.connect.rest.util.ByteUtils;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Copyright (C) 2010 RapidPM
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* <p>
20+
* Created by m.lang - RapidPM - Team on 12.07.2017.
21+
*/
22+
public class ByteUtilsTest {
23+
24+
@Test
25+
public void test001() throws Exception {
26+
final byte[] testByteArray = "testByteArray".getBytes();
27+
final Byte[] nonPrimitive = ByteUtils.fromPrimitive(testByteArray);
28+
assertEquals(nonPrimitive.length, testByteArray.length);
29+
30+
final byte[] primitive = ByteUtils.toPrimitive(nonPrimitive);
31+
assertEquals(primitive.length, testByteArray.length);
32+
assertEquals("testByteArray", new String(primitive));
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package junit.org.rapidpm.binarycache.connect.rest.v007;
2+
3+
import junit.org.rapidpm.binarycache.connect.rest.BaseBinaryCacheRestTest;
4+
import org.junit.Assert;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.TemporaryFolder;
8+
import org.rapidpm.binarycache.api.defaultkey.DefaultCacheKey;
9+
import org.rapidpm.binarycache.connect.rest.BinaryCacheRestClient;
10+
import org.rapidpm.ddi.DI;
11+
12+
import javax.ws.rs.core.Response;
13+
import java.io.File;
14+
import java.io.FileInputStream;
15+
16+
/**
17+
* Copyright (C) 2010 RapidPM
18+
* Licensed under the Apache License, Version 2.0 (the "License");
19+
* you may not use this file except in compliance with the License.
20+
* You may obtain a copy of the License at
21+
* http://www.apache.org/licenses/LICENSE-2.0
22+
* Unless required by applicable law or agreed to in writing, software
23+
* distributed under the License is distributed on an "AS IS" BASIS,
24+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
* See the License for the specific language governing permissions and
26+
* limitations under the License.
27+
* <p>
28+
* Created by m.lang - RapidPM - Team on 12.07.2017.
29+
*/
30+
public class BinaryCacheRestClientTest007 extends BaseBinaryCacheRestTest {
31+
32+
@Rule
33+
public TemporaryFolder folder = new TemporaryFolder();
34+
35+
@Test
36+
public void test001() throws Exception {
37+
final BinaryCacheRestClient client = DI.activateDI(BinaryCacheRestClient.class);
38+
final DefaultCacheKey key = new DefaultCacheKey("123");
39+
final File file = folder.newFile();
40+
final FileInputStream inputStream = new FileInputStream(file);
41+
inputStream.close();
42+
final Response response = client.cacheBinary("notThere", encodedKey(key), inputStream);
43+
Assert.assertEquals(500, response.getStatus());
44+
}
45+
46+
@Test
47+
public void test002() throws Exception {
48+
final BinaryCacheRestClient client = DI.activateDI(BinaryCacheRestClient.class);
49+
final DefaultCacheKey key = new DefaultCacheKey("123");
50+
final File file = folder.newFile();
51+
final FileInputStream inputStream = new FileInputStream(file);
52+
inputStream.close();
53+
final Response response = client.cacheBinaryIfAbsent("notThere", encodedKey(key), inputStream);
54+
Assert.assertEquals(500, response.getStatus());
55+
}
56+
57+
}

modules/app/cache/provider/ehcache/src/main/java/org/rapidpm/binarycache/provider/ehcache/EhCacheImpl.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import javax.annotation.PreDestroy;
1010
import javax.cache.Cache;
1111
import javax.cache.Caching;
12+
import javax.cache.configuration.Configuration;
13+
import javax.cache.configuration.MutableConfiguration;
14+
import javax.cache.expiry.AccessedExpiryPolicy;
15+
import javax.cache.expiry.Duration;
1216
import javax.cache.spi.CachingProvider;
17+
import java.io.File;
18+
import java.net.URI;
1319
import java.net.URISyntaxException;
1420

1521
/**
@@ -28,23 +34,51 @@
2834
*/
2935
public class EhCacheImpl implements BinaryCacheClient {
3036

31-
public static final String CONFIG_EHCACHE_XML = "/config/ehcache.xml";
37+
public static final String CACHE_CONFIG_PROPERTY = "binarycache.config";
38+
private static final String CONFIG_EHCACHE_XML = "/config/ehcache.xml";
3239
private javax.cache.CacheManager cacheManager;
3340

3441
@PostConstruct
3542
public void init() throws URISyntaxException {
3643
final CachingProvider cachingProvider = Caching.getCachingProvider();
3744
cacheManager = cachingProvider.getCacheManager(
38-
getClass().getResource(CONFIG_EHCACHE_XML).toURI(),
45+
getUriToConfig(),
3946
getClass().getClassLoader()
4047
);
4148
}
4249

50+
private URI getUriToConfig() throws URISyntaxException {
51+
final String property = System.getProperty(CACHE_CONFIG_PROPERTY);
52+
URI uri;
53+
if (property != null && !property.isEmpty()) {
54+
uri = new File(property).toURI();
55+
} else {
56+
uri = getClass().getResource(CONFIG_EHCACHE_XML).toURI();
57+
}
58+
return uri;
59+
}
60+
4361
@PreDestroy
4462
public void destroy() {
4563
cacheManager.close();
4664
}
4765

66+
@Override
67+
public Cache<CacheKey, CacheByteArray> createCache(String cacheName) {
68+
MutableConfiguration<CacheKey, CacheByteArray> config
69+
= new MutableConfiguration<CacheKey, CacheByteArray>()
70+
.setTypes(CacheKey.class, CacheByteArray.class)
71+
.setExpiryPolicyFactory(
72+
AccessedExpiryPolicy.factoryOf(Duration.ONE_HOUR))
73+
.setStatisticsEnabled(true);
74+
return cacheManager.createCache(cacheName, config);
75+
}
76+
77+
@Override
78+
public Cache<CacheKey, CacheByteArray> createCache(String cacheName, Configuration<CacheKey, CacheByteArray> configuration) {
79+
return cacheManager.createCache(cacheName, configuration);
80+
}
81+
4882
@Override
4983
public Cache<CacheKey, CacheByteArray> getCache(String cacheName) {
5084
return cacheManager.getCache(cacheName, CacheKey.class, CacheByteArray.class);
@@ -55,6 +89,4 @@ public Result removeEntry(String cacheName, CacheKey cacheKey) {
5589
return cacheManager.getCache(cacheName, CacheKey.class, CacheByteArray.class).remove(cacheKey) ? Result.OK : Result.FAILED;
5690
}
5791

58-
59-
6092
}

modules/app/cache/provider/ehcache/src/main/resources/config/ehcache.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<ehcache:config
2-
xmlns:ehcache="http://www.ehcache.org/v3"
3-
xmlns:jcache="http://www.ehcache.org/v3/jsr107">
2+
xmlns:ehcache="http://www.ehcache.org/v3">
43

54
<ehcache:cache alias="default" uses-template="defaultTemplate"/>
5+
<ehcache:cache alias="myCache" uses-template="defaultTemplate"/>
66

77
<ehcache:cache-template name="defaultTemplate">
88
<ehcache:key-type>org.rapidpm.binarycache.api.CacheKey</ehcache:key-type>
99
<ehcache:value-type>org.rapidpm.binarycache.api.CacheByteArray</ehcache:value-type>
1010
<ehcache:expiry>
11-
<ehcache:tti unit="minutes">30</ehcache:tti>
11+
<!-- tti=time to idle; ttl=time to live; -->
12+
<ehcache:tti unit="hours">1</ehcache:tti>
1213
</ehcache:expiry>
14+
1315
<ehcache:heap unit="entries">200</ehcache:heap>
1416
</ehcache:cache-template>
1517

0 commit comments

Comments
 (0)