使用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 ...
随机推荐
- 论文泛读《T-Miner: A Generative Approach to Defend Against Trojan Attacks on DNN-based Text Classification》
发表时间:2021 期刊会议:30th USENIX Security Symposium 论文单位:Virginia Tech 论文作者:Ahmadreza Azizi,Ibrahim Asadul ...
- [NET,C# ] Nuget包发布流程
1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...
- 《前端运维》五、k8s--4机密信息存储与统一管理服务环境变量
一.储存机密信息 Secret 是 Kubernetes 内的一种资源类型,可以用它来存放一些机密信息(密码,token,密钥等).信息被存入后,我们可以使用挂载卷的方式挂载进我们的 Pod 内.当然 ...
- 《前端运维》五、k8s--2pod、services与Ingress部署
前一篇啊,我们学完了基本的配置.这一篇,我们来看下服务部署的配置.我们先来看张图,理解下k8s的应用场景和调用流程: 看上图,首先,master是控制节点,负责编排.管理.调度用户提交的作业.kube ...
- 无法加载文件 C:\Users\admin\AppData\Roaming\npm\anyproxy.ps1
无法加载文件 C:\Users\admin\AppData\Roaming\npm\anyproxy.ps1 我们右击开始菜单,以管理员身份打开power shell 运行命令: set-Execut ...
- 零售经营“新赛道” ——基于手机银行APP专区调研的客群精细化运营分析报告
随着银行业竞争的不断深入及新客户增量日渐"到顶",各家银行的客群竞争逐渐由"跑马圈地"进入"精耕细作"的新阶段,在客群精准化服务方面不断 ...
- ChatGPT生成接口测试用例(一)
接口测试在软件开发生命周期中扮演着至关重要的角色,有助于验证不同模块之间的交互是否正确.若协议消息被恶意修改,系统是否能够恰当处理,以确保系统的功能正常运行,不会出现宕机或者安全问题. 5.1 Cha ...
- Java 基于接口的动态代理
UserDao接口 package com.pry.cn; public interface UserDao { public int add(int a,int b); public String ...
- ctr命令的基本使用与技巧
k8s早1.24后放弃docker,并把containerd作为运行时组件,containerd 调用链更短,组件更少,更稳定,占用节点资源更少 ctr是containerd的一个客户端工具 cric ...
- shell判断字符串结尾
下面围绕"判断字符串是否以.txt结尾"展开.转变一下也同样适用于"判断字符串是否以.txt开头". 通用的方法 # 方法一.使用grep命令 #!/bin/s ...