本文整理汇总了Java中org.ehcache.Cache类的典型用法代码示例。如果您正苦于以下问题:Java Cache类的具体用法?Java Cache怎么用?Java Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cache类属于org.ehcache包,在下文中一共展示了Cache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.ehcache.Cache; //导入依赖的package包/类
public static void main(String[] args) {
LOGGER.info("Creating cache manager via XML resource");
Configuration xmlConfig = new XmlConfiguration(BasicXML.class.getResource("/ehcache.xml"));
try (CacheManager cacheManager = newCacheManager(xmlConfig)) {
cacheManager.init();
Cache<Long, String> basicCache = cacheManager.getCache("basicCache", Long.class, String.class);
LOGGER.info("Putting to cache");
basicCache.put(1L, "da one!");
String value = basicCache.get(1L);
LOGGER.info("Retrieved '{}'", value);
LOGGER.info("Closing cache manager");
}
LOGGER.info("Exiting");
}
示例2: doGetAuthenticationInfo
import org.ehcache.Cache; //导入依赖的package包/类
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
String token = (String) auth.getCredentials();
Cache<String, String> authCache = CacheController.getAuthCache();
if (! authCache.containsKey(token)) {
// get user info from database
int uid = JWTUtil.getUid(token);
UserEntity userEntity = userService.getUserByUid(uid);
authCache.put(token, String.valueOf(userEntity.getPassword()));
}
String secret = authCache.get(token);
if (!JWTUtil.decode(token, secret)) {
throw new AuthenticationException("Token invalid");
}
return new SimpleAuthenticationInfo(token, token, "jwt_realm");
}
示例3: login
import org.ehcache.Cache; //导入依赖的package包/类
@ApiOperation("用户登入")
@PostMapping("/login")
public ResponseEntity login(@RequestBody @Valid IndexLoginFormat format) {
UserEntity userEntity = userService.getUserByLogin(format.getEmail(),
new Md5Hash(format.getPassword()).toString());
if (userEntity == null)
throw new WebErrorException("用户名密码错误");
JSONArray array = userEntity.getPermission();
Iterator<Object> it = array.iterator();
Set<String> permission = new HashSet<>();
while (it.hasNext()) {
permission.add(it.next().toString());
}
String token = JWTUtil.sign(userEntity.getUid(), userEntity.getRole(), permission, userEntity.getPassword());
Cache<String, String> authCache = CacheController.getAuthCache();
authCache.put(token, userEntity.getPassword());
return new ResponseEntity("登入成功", token);
}
示例4: testTem
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testTem() {
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("preConfigured",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)))
.build();
cacheManager.init();
Cache<Long, String> preConfigured =
cacheManager.getCache("preConfigured", Long.class, String.class);
Cache<Long, String> myCache = cacheManager.createCache("myCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)));
myCache.put(1L, "da one!");
myCache.putIfAbsent(0L, "ee");
String value = myCache.get(1L);
System.out.println("Value is " + value);
cacheManager.removeCache("preConfigured");
cacheManager.close();
}
示例5: findOperationStat
import org.ehcache.Cache; //导入依赖的package包/类
public static OperationStatistic findOperationStat(Cache<?, ?> cache1, final String statName, final String tag) {
Query q = queryBuilder()
.descendants().filter(context(identifier(subclassOf(OperationStatistic.class)))).build();
Set<TreeNode> operationStatisticNodes = q.execute(Collections.singleton(ContextManager.nodeFor(cache1)));
Set<TreeNode> result = queryBuilder()
.filter(
context(attributes(Matchers.<Map<String, Object>>allOf(
hasAttribute("name", statName), hasAttribute("tags", new Matcher<Set<String>>() {
@Override
protected boolean matchesSafely(Set<String> object) {
return object.contains(tag);
}
}))))).build().execute(operationStatisticNodes);
if (result.size() != 1) {
throw new RuntimeException("single stat not found; found " + result.size());
}
TreeNode node = result.iterator().next();
return (OperationStatistic) node.getContext().attributes().get("this");
}
示例6: findValueStat
import org.ehcache.Cache; //导入依赖的package包/类
public static ValueStatistic findValueStat(Cache<?, ?> cache1, final String statName, final String tag) {
Query q = queryBuilder().chain(self())
.descendants().filter(context(identifier(subclassOf(ValueStatistic.class)))).build();
Set<TreeNode> nodes = q.execute(Collections.singleton(ContextManager.nodeFor(cache1)));
Set<TreeNode> result = queryBuilder()
.filter(
context(attributes(Matchers.<Map<String, Object>>allOf(
hasAttribute("name", statName), hasAttribute("tags", new Matcher<Set<String>>() {
@Override
protected boolean matchesSafely(Set<String> object) {
return object.contains(tag);
}
}))))).build().execute(nodes);
if (result.size() != 1) {
throw new RuntimeException("single stat not found; found " + result.size());
}
TreeNode node = result.iterator().next();
return (ValueStatistic) node.getContext().attributes().get("this");
}
示例7: main
import org.ehcache.Cache; //导入依赖的package包/类
public static void main(String[] args) {
LOGGER.info("Creating cache manager programmatically");
try (CacheManager cacheManager = newCacheManagerBuilder()
.withCache("basicCache",
newCacheConfigurationBuilder(Long.class, String.class, heap(100).offheap(1, MB)))
.build(true)) {
Cache<Long, String> basicCache = cacheManager.getCache("basicCache", Long.class, String.class);
LOGGER.info("Putting to cache");
basicCache.put(1L, "da one!");
String value = basicCache.get(1L);
LOGGER.info("Retrieved '{}'", value);
LOGGER.info("Closing cache manager");
}
LOGGER.info("Exiting");
}
示例8: main
import org.ehcache.Cache; //导入依赖的package包/类
public static void main(String[] args) {
LOGGER.info("Creating clustered cache manager");
final URI uri = create("terracotta://localhost:9510/clustered");
try (CacheManager cacheManager = newCacheManagerBuilder()
.with(cluster(uri).autoCreate().defaultServerResource("default-resource"))
.withCache("basicCache",
newCacheConfigurationBuilder(Long.class, String.class,
heap(100).offheap(1, MB).with(clusteredDedicated(5, MB))))
.build(true)) {
Cache<Long, String> basicCache = cacheManager.getCache("basicCache", Long.class, String.class);
LOGGER.info("Putting to cache");
basicCache.put(1L, "da one!");
LOGGER.info("Closing cache manager");
}
LOGGER.info("Exiting");
}
示例9: main
import org.ehcache.Cache; //导入依赖的package包/类
public static void main(String[] args) {
LOGGER.info("Creating clustered cache manager from XML");
final URL myUrl = ClusteredXML.class.getResource("/ehcache.xml");
Configuration xmlConfig = new XmlConfiguration(myUrl);
try (CacheManager cacheManager = newCacheManager(xmlConfig)) {
cacheManager.init();
Cache<Long, String> basicCache = cacheManager.getCache("basicCache", Long.class, String.class);
LOGGER.info("Getting from cache");
String value = basicCache.get(1L);
LOGGER.info("Retrieved '{}'", value);
LOGGER.info("Closing cache manager");
}
LOGGER.info("Exiting");
}
示例10: getApiIdCache
import org.ehcache.Cache; //导入依赖的package包/类
public Cache<String, String> getApiIdCache(String protocolAndMethod) {
if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpGet;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpPost;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpPut;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpDelete;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpsGet;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpsPost;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpsPut;
} else if (protocolAndMethod.equals(Const.API_MATCH_HTTP_GET_MAP)) {
return apiMatchHttpsDelete;
}
return null;
}
示例11: testCachePut
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testCachePut() throws Exception {
final Cache<Object, Object> cache = getTestCache();
final String key = generateRandomString();
final String val = generateRandomString();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedBodiesReceived(val);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, false);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);
fluentTemplate()
.withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_PUT)
.withHeader(EhcacheConstants.KEY, key)
.withBody(val)
.to("direct://start")
.send();
assertMockEndpointsSatisfied();
assertTrue(cache.containsKey(key));
assertEquals(val, cache.get(key));
}
示例12: testCachePutAll
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testCachePutAll() throws Exception {
final Cache<Object, Object> cache = getTestCache();
final Map<String, String> map = generateRandomMapOfString(3);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, false);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);
fluentTemplate()
.withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_PUT_ALL)
.withBody(map)
.to("direct://start")
.send();
assertMockEndpointsSatisfied();
map.forEach((k, v) -> {
assertTrue(cache.containsKey(k));
assertEquals(v, cache.get(k));
});
}
示例13: testCacheGet
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testCacheGet() throws Exception {
final Cache<Object, Object> cache = getTestCache();
final String key = generateRandomString();
final String val = generateRandomString();
cache.put(key, val);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedBodiesReceived(val);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, true);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);
fluentTemplate()
.withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_GET)
.withHeader(EhcacheConstants.KEY, key)
.withBody(val)
.to("direct://start")
.send();
assertMockEndpointsSatisfied();
}
示例14: testCacheRemove
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testCacheRemove() throws Exception {
final Cache<Object, Object> cache = getTestCache();
final String key = generateRandomString();
final String val = generateRandomString();
cache.put(key, val);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, false);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);
fluentTemplate()
.withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_REMOVE)
.withHeader(EhcacheConstants.KEY, key)
.to("direct://start")
.send();
assertMockEndpointsSatisfied();
assertFalse(cache.containsKey(key));
}
示例15: testCacheRemoveAll
import org.ehcache.Cache; //导入依赖的package包/类
@Test
public void testCacheRemoveAll() throws Exception {
final Cache<Object, Object> cache = getTestCache();
final Map<String, String> map = generateRandomMapOfString(3);
final Set<String> keys = map.keySet().stream().limit(2).collect(Collectors.toSet());
cache.putAll(map);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(1);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, false);
mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);
fluentTemplate()
.withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_REMOVE_ALL)
.withHeader(EhcacheConstants.KEYS, keys)
.to("direct://start")
.send();
assertMockEndpointsSatisfied();
cache.forEach(e -> assertFalse(keys.contains(e.getKey())));
}