使用guava的cache实现缓存
一、maven
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sshcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sshcache</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.3-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、代码
import ch.ethz.ssh2.Connection;
import com.google.common.base.Joiner;
import com.google.common.cache.*;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class SshConnectionUtil {
private static LoadingCache<String,Connection> sshLoadingCache = null;
static {
sshLoadingCache = CacheBuilder.newBuilder()
.expireAfterAccess(5,TimeUnit.MINUTES)
.maximumSize(1000)
.removalListener(new RemovalListener<String, Connection>(){
@Override
public void onRemoval(RemovalNotification<String, Connection> removalNotification) {
if(!Objects.isNull(removalNotification.getValue())){
removalNotification.getValue().close();
removalNotification.setValue(null);
}
String key = removalNotification.getKey();
System.out.println("ip:" + key.substring(0,key.indexOf(',')) + " removed");
}
} )
.build(new CacheLoader<String, Connection>() {
@Override
public Connection load(String key) {
//从SQL或者NoSql 获取对象
//Iterable<String> split = Splitter.on(',').split(key);
String[] split = key.split(",");
String username = split.length == 3 ? split[2] : "root";
String password = split[1];
String ip = split[0];
Connection connection = new Connection(ip, 22);
boolean b = false;
try {
connection.addConnectionMonitor(v -> {
System.out.println(v);
});
b = connection.authenticateWithPassword(username, password);
if(b) {
return connection;
}else {
return null;
}
}catch (IOException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
if(!b){
connection.close();
}
}
return null;
}
});
}
public static Connection getConnection(String ip,String password,String username){
String join = Joiner.on(',').skipNulls().join(ip, password, username);
try {
Connection connection = sshLoadingCache.get(join);
return connection;
}catch (ExecutionException e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Connection root = SshConnectionUtil.getConnection("192.168.11.110", "123456", "root");
System.out.println(root);
}
}
博客原文:https://blog.csdn.net/xiaolixi199311/article/details/117902680
使用guava的cache实现缓存的更多相关文章
- Guava Cache本地缓存
Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...
- Guava Cache 本地缓存组件浅析
cache组件中核心的类和接口列举如下: 接口: Cache 本地缓存的顶级接口,提供一些对缓存进行get,put的方法,以及获取缓存统计数据的方法等. LoadingCache 继承了Cache接口 ...
- Google Guava之--cache
一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...
- guava学习--cache
转载:http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache http://www.cnblogs.com/parryyang/ ...
- System.Web.Caching.Cache类 缓存
1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...
- Cache类缓存
此处主要总结System.Web.Caching.Cache类 该类是用于存储常用信息的类,HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 该 ...
- Linux 释放cache化缓存
Linux 释放cache化缓存 free -g查看空余内存以及已使用内存 原文 https://blog.csdn.net/tomspcc/article/details/78131468 机械硬 ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...
- Linux的Cache Memory(缓存内存)机制
转:https://blog.csdn.net/kaikai_sk/article/details/79177036 PS:为什么Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与W ...
随机推荐
- Threejs入门-灯光
在 Three.js 中,灯光是非常重要的元素之一,它能够模拟现实世界中的光照效果,帮助我们打造更加真实的三维场景.灯光的种类和配置方式可以影响整个场景的视觉效果,在不同的应用中,灯光的使用非常关键. ...
- AE对象序列化
当我们编写AE程序时,通常会遇到需要存储某个AE对象的情况,比如Layer,Element,Map,Legend,NorthArrow等等这些.举个例子说明一下:在我们编辑Featurelayer时, ...
- C++ Builder 开发64程序 使用AnsiString的ToInt和ToDouble会内存泄漏
AnsiString str="adsfaga"; try { int v=str.ToInt(); } catch(...) { } 上面的代码,在C++ Builder 10 ...
- 【Amadeus原创】GFS 安装使用
准备环境: 一.修改每台主机的host文件,在/etc/hosts下添加内容: 172.62.21.44 gfs1 172.62.21.43 gfs2 172.62.21.37 gfs3 二.分别安装 ...
- 断言、drf之请求与响应
目录 一.断言 二.drf之请求 2.1 Request能够解析的前端传入的编码格式 2.2 Request类有哪些属性和方法(学过) 常用参数 Response类的实例化参数 三.drf之响应 3. ...
- GOLAND-激活码-20230309
33MEHOB8W0-eyJsaWNlbnNlSWQiOiIzM01FSE9COFcwIiwibGljZW5zZWVOYW1lIjoiUG9saXRla25payBNZXJsaW1hdSBNZWxha ...
- MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded
OUTLINE问题描述解决方案问题描述在mac下,用sequel pro连接数据库,出现以下问题: MySQL said: Authentication plugin 'caching_sha2_pa ...
- Win10正式专业版激活方法
首先,我们先查看一下Win10正式专业版系统的激活状态: 点击桌面左下角的"Windows"按钮,从打开的扩展面板中依次点击"设置"-"更新和安全 ...
- Qt安卓开发经验021-030
关于权限设置,在早期的安卓版本,所有权限都写在全局配置文件AndroidManifest.xml中,这种叫安装时权限,就是安装的时候告诉安卓系统当前app需要哪些权限.大概从安卓6开始,部分权限需要动 ...
- 多语言越狱很棒!😊MULTILINGUAL JAILBREAK CHALLENGES IN🧜♀️LARGE LANGUAGE MODELS
写这篇的时候第一次认识到了附录内容的重要性(bushi只是这篇读了全部的附录),它可以让你明确文章全部的实验,并且其中包含很多辅助你对正文理解的表格 禁止盗用,侵权必究!!!欢迎大家积极举报