package com.pjk.demo; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.TimeUnit; /** * @author PJK */ @Slf4j public class DemoGuavaCache { public static void main(String[] args) { // 创建一个cache实例 Cache<String, String> cache = CacheBuilder.newBuilder() //最大缓存大小 .maximumSize(100) //写入后的过期时间 .expireAfterWrite(10, TimeUnit.MINUTES) .build(); cache.put("name", "zhangsan"); String cacheValue = cache.getIfPresent("name"); log.info("name缓存对应的值为:{}", cacheValue); String cacheAge = cache.getIfPresent("age"); log.info("第一次查询age缓存对应的值为:{}", cacheAge); if (cacheAge == null) { //缓存中没有这个key 应该去查询这个key的值 然后添加到缓存中 cache.put("age", "18"); cacheAge = cache.getIfPresent("age"); log.info("第二次查询age缓存对应的值为:{}", cacheAge); } } }
最近要用到缓存,一直想看又没看,今晚睡不着,简单看了下,主要流程如下
1.添加guava依赖
2.创建cache实例
3.使用cache.put()方法像缓存中添加数据
4.使用cache.getIfPresent()方法获取缓存
5.使用cache.get()方法来给不存在的数据进行添加操作
同时想到了一个场景
可以创建多个缓存对象 然后对着多个缓存对象进行操作吗? 比如我有3种数据,每种10万条,我需要将3种数据放到缓存中,然后从缓存的三种数据中,查询有关系的数据,将他们拼成一条记录 存到mysql中 怎么做
结果
import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.concurrent.TimeUnit; public class DemoCache { private static Cache<String, String> cache1 = CacheBuilder.newBuilder() .maximumSize(100000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(); private static Cache<String, String> cache2 = CacheBuilder.newBuilder() .maximumSize(100000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(); private static Cache<String, String> cache3 = CacheBuilder.newBuilder() .maximumSize(100000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(); public static void main(String[] args) { // 存储数据到缓存中 for (int i = 1; i <= 100000; i++) { storeData(cache1, "key1-" + i, "value1-" + i); storeData(cache2, "key2-" + i, "value2-" + i); storeData(cache3, "key3-" + i, "value3-" + i); } // 从缓存中获取数据,并将相关数据拼接后存储到MySQL中 for (int i = 1; i <= 100000; i++) { String key1 = "key1-" + i; String key2 = "key2-" + i; String key3 = "key3-" + i; String value1 = getData(cache1, key1); String value2 = getData(cache2, key2); String value3 = getData(cache3, key3); // 将数据拼接后存储到MySQL中 saveToMySQL(key1, key2, key3, value1, value2, value3); } } public static void storeData(Cache<String, String> cache, String key, String value) { cache.put(key, value); } public static String getData(Cache<String, String> cache, String key) { return cache.getIfPresent(key); } public static void saveToMySQL(String key1, String key2, String key3, String value1, String value2, String value3) { try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); PreparedStatement statement = connection.prepareStatement("INSERT INTO mytable (key1, key2, key3, value1, value2, value3) VALUES (?, ?, ?, ?, ?, ?)")) { statement.setString(1, key1); statement.setString(2, key2); statement.setString(3, key3); statement.setString(4, value1); statement.setString(5, value2); statement.setString(6, value3); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } }
在上述示例中,我们创建了3个缓存对象 cache1、cache2 和 cache3,分别用于存储三种数据。使用循环将100,000条数据存储到每个缓存中。
然后,我们通过循环从缓存中获取数据,并将相关数据拼接后存储到MySQL中。在 saveToMySQL() 方法中,我们使用 JDBC 连接到 MySQL 数据库,并执行插入操作,将数据存储到 mytable 表中。