一、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. Prometheus之系统安装,启动

    Prometheus简介Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包. 自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人 ...

  2. 2023-05 多校联合训练 HZNU站

    我想要原石 然而,由于提瓦特大陆实在是太大了,游戏中设置了许多传送锚点.众所周知,每个传送锚点附近都有若干个原石(其实并没有),曾经有一位丰富经验的旅行者开辟了 \(n−1\) 条路和 \(n\) 个 ...

  3. 微服务-SpringBoot

    基础知识 微服务主旨就是将一个大型系统拆分为多个小型服务. 多个服务之间可以是异构的.单体服务在大型项目下很难维护. 智能端点与哑管道:就是消息之间通信只传送消息,而不做校验. SpringCloud ...

  4. React使用useRef调用子组件方法

    前情 公司前端主技术栈是react系,最近在提取组件的时候想到vue可以通过ref获取子组件,再调用子组件的方法,于是想在react中实现同样效果. 实现原理 父组件调用useRef获取ref对象,再 ...

  5. 人工智能大语言模型起源篇,低秩微调(LoRA)

    上一篇: <规模法则(Scaling Law)与参数效率的提高> 序言:您在找工作时会不会经常听到LoRA微调,这项技术的来源就是这里了. (12)Hu.Shen.Wallis.Allen ...

  6. LSTM学习三维轨迹的Python实现

    一.引言 长短期记忆网络(LSTM)是一种强大的递归神经网络(RNN),广泛应用于时间序列预测.自然语言处理等任务.在处理具有时间序列特征的数据时,LSTM通过引入记忆单元和门控机制,能够更有效地捕捉 ...

  7. .NET周刊【12月第2期 2024-12-08】

    国内文章 终于解决了.net在线客服系统总是被360误报的问题(对软件进行数字签名) https://www.cnblogs.com/sheng_chao/p/18581139 升讯威在线客服与营销系 ...

  8. Jetpack Compose学习(15)——Pager组件的使用(对标ViewPager)

    原文地址: Jetpack Compose学习(15)--Pager组件的使用(对标ViewPager)-Stars-One的杂货小窝 从名字可以看出,Pager这个就是ViewPager的替代产物 ...

  9. 扩容ext4分区容量16TB限制

    #扩容ext4分区容量16TB限制 环境: 系统 ubuntu 16 resize2fs 1.42.13 (17-May-2015) 使用resize2fs扩容时如下提示 resize2fs /dev ...

  10. docker save与docker export实现docker镜像与容器的备份

    本来想写一篇关于docker save/export/commit/load/import之间的关系的文章,后来看了看,已经有很多人写过了,我就不做重复工作了. 参见: docker save与doc ...