Skip to content

Commit

Permalink
[KARAF-6017] - added cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
awrb committed Oct 25, 2022
1 parent 8133668 commit 4a73c63
Show file tree
Hide file tree
Showing 14 changed files with 609 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cache/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<parent>
<artifactId>cache</artifactId>
<groupId>org.apache.karaf.cache</groupId>
<version>4.4.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>api</artifactId>

<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.apache.karaf.cache.api;

import javax.cache.configuration.Configuration;
import java.util.List;

public interface CacheService {

<K, V> void createCache(String name, Configuration<K, V> configuration);

<K, V> V get(String name, K key);

<K, V> void put(String name, K key, V value);

void invalidateCache(String name);

List<String> listCaches();
}
65 changes: 65 additions & 0 deletions cache/commands/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<parent>
<artifactId>cache</artifactId>
<groupId>org.apache.karaf.cache</groupId>
<version>4.4.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>commands</artifactId>

<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.karaf.cache</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-services-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Karaf-Commands>*</Karaf-Commands>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.apache.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

import javax.cache.configuration.Configuration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;

@Service
@Command(scope = "cache", name = "create", description = "")
public class Create implements Action {

@Argument(index = 0, required = true)
String cacheName;
@Argument(index = 1, required = true)
String keyType;
@Argument(index = 2, required = true)
String valueType;
@Reference
private CacheService cacheService;

@Override
public Object execute() throws Exception {
Class keyClass = Class.forName(keyType);
Class valueClass = Class.forName(valueType);
Configuration<?, ?> configuration = new MutableConfiguration<>()
.setTypes(keyClass, valueClass)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
cacheService.createCache(cacheName, configuration);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.apache.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.cache.core.commands.completers.CacheNameCompleter;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Service
@Command(scope = "cache", name = "get", description = "")
public class Get implements Action {

@Reference
CacheService cacheService;

@Argument(index = 0, required = true)
@Completion(CacheNameCompleter.class)
String cacheName;

@Argument(index = 1, required = true)
Object key;

@Override
public Object execute() throws Exception {
return cacheService.get(cacheName, key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.apache.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.cache.core.commands.completers.CacheNameCompleter;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Service
@Command(scope = "cache", name = "invalidate", description = "")
public class Invalidate implements Action {

@Reference
CacheService cacheService;

@Argument(index = 0, required = true)
@Completion(CacheNameCompleter.class)
String cacheName;

@Override
public Object execute() throws Exception {
cacheService.invalidateCache(cacheName);
return cacheService + " invalidated";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.apache.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Service
@Command(scope = "cache", name = "list", description = "")
public class ListCaches implements Action {

@Reference
CacheService cacheService;

@Override
public Object execute() throws Exception {
return cacheService.listCaches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.apache.karaf.cache.core.commands;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.cache.core.commands.completers.CacheNameCompleter;
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;

@Service
@Command(scope = "cache", name = "put", description = "")
public class Put implements Action {

@Reference
private CacheService cacheService;

@Argument(index = 0, required = true)
@Completion(CacheNameCompleter.class)
String cacheName;

@Argument(index = 1, required = true)
Object key;

@Argument(index = 2, required = true)
Object value;

@Override
public Object execute() throws Exception {
cacheService.put(cacheName, key, value);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.apache.karaf.cache.core.commands.completers;

import org.apache.karaf.cache.api.CacheService;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.CommandLine;
import org.apache.karaf.shell.api.console.Completer;
import org.apache.karaf.shell.api.console.Session;
import org.apache.karaf.shell.support.completers.StringsCompleter;

import java.util.List;

@Service
public class CacheNameCompleter implements Completer {

@Reference
CacheService cacheService;

@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
StringsCompleter delegate = new StringsCompleter();
try {
for (String cache : cacheService.listCaches()) {
delegate.getStrings().add(cache);
}
} catch (Exception e) {
// nothing to do
}

return delegate.complete(session, commandLine, candidates);
}
}

0 comments on commit 4a73c63

Please sign in to comment.