一、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实现缓存的更多相关文章

  1. Guava Cache本地缓存

    Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...

  2. Guava Cache 本地缓存组件浅析

    cache组件中核心的类和接口列举如下: 接口: Cache 本地缓存的顶级接口,提供一些对缓存进行get,put的方法,以及获取缓存统计数据的方法等. LoadingCache 继承了Cache接口 ...

  3. Google Guava之--cache

    一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...

  4. guava学习--cache

    转载:http://outofmemory.cn/java/guava/cache/how-to-use-guava-cache   http://www.cnblogs.com/parryyang/ ...

  5. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

  6. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  7. Cache类缓存

    此处主要总结System.Web.Caching.Cache类 该类是用于存储常用信息的类,HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 该 ...

  8. Linux 释放cache化缓存

    Linux 释放cache化缓存 free -g查看空余内存以及已使用内存 原文  https://blog.csdn.net/tomspcc/article/details/78131468 机械硬 ...

  9. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  10. Linux的Cache Memory(缓存内存)机制

    转:https://blog.csdn.net/kaikai_sk/article/details/79177036 PS:为什么Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与W ...

随机推荐

  1. vue项目中如何加载markdown

    场景 今天忽然临时接到一个需求: 就是将markdown文件直接在vue项目中进行加载,并正常显示出来. 这......,我知道是可以进行加载markdown文件的. 但是我之前没有做过,答复的是:可 ...

  2. BitLocker驱动器加锁和解锁

    应用场景: 单位配备给你使用的电脑(Win10系统),偶尔也会有其他人使用.你可以设置某一个磁盘为你的私密数据存储空间,只有你输入密码后才能进入磁盘.即使系统重装.硬盘被拆下来挂载到其他机器上,没有密 ...

  3. 通过NPOI读取 excel指定Sheet 到 DataTable

    public static DataTable ReadExcelToDataTable(string fileName, string sheetName = null, bool isFirstR ...

  4. This request has been blocked; the content must be served over HTTPS.此请求已被阻止;内容必须通过HTTPS提供

    近期在做将高德地图替换为天地图时遇到了这个问题,原因是https由于安全协议禁止了http的请求 http://api.tianditu.gov.cn/api?v=4.0&tk=XXXXXXX ...

  5. Java基础 —— 泛型

    泛型  泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 理解  为了可以进一步理解泛型,我们先来看一个问题 需求:  编写一个程序,在ArrayList中添加三个对象,类 ...

  6. vue 使用 application/x-www-form-urlencoded格式提交数据

    const params = new URLSearchParams();//前端在传参时需要先新建一个URLSearchParams对象,然后将参数append到这个对象中 params.appen ...

  7. 数据万象推出智能检索MetaInsight,现已开启限时公测

    海量文件的分析统计一直是对象存储COS的热点需求,伴随AIGC飞速迭代发展,在众多不同模态素材的海洋中,用户也急需更高效地管理和利用多媒体内容,打破传统搜索的桎梏. 数据万象推出的智能检索 MetaI ...

  8. Flutter TickerProvider使用

    Flutter TickerProvider使用 当需要使用Animation controller时,需要在控制器初始化时传递一个vsync参数,此时需要用到TickerProvider,例如 cl ...

  9. windows 也支持右键复制文件名了

    mac 有一个操作,alt + 右键,出现的菜单有复制路径一项.不用羡慕,现在 windows 也有这个功能了. Shift + 右键,"复制为路径":

  10. HBaseIntrospector

    package x;import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; ...