缓存List并写入文件持久化
程序员文章站
2022-06-24 11:06:08
LIfe is half spent before we know what is it. 缓存List并写入文件持久化 需要缓存一个List集合,比如缓存一个输入框中用户之前输入过的内容,下次当用户再想往输入框中写东西就调用这个缓存的接口,把之前输入过的东西从缓存中取出并返回; 而为了这么一个小小 ......
life is half spent before we know what is it.
缓存list并写入文件持久化
需要缓存一个list集合,比如缓存一个输入框中用户之前输入过的内容,下次当用户再想往输入框中写东西就调用这个缓存的接口,把之前输入过的东西从缓存中取出并返回;
而为了这么一个小小的缓存,引入第三方库redis或是cache等也不太划算,于是就可以把缓存写入文件中,序列化到java虚拟机中,这样就可以支持持久化,即使重启项目缓存的list依然存在;
缓存工具类:
1 package com.xinyan.springcloud.tjt; 2 3 4 import java.io.file; 5 import java.io.fileinputstream; 6 import java.io.fileoutputstream; 7 import java.io.ioexception; 8 import java.io.objectinputstream; 9 import java.io.objectoutputstream; 10 import java.io.serializable; 11 import java.util.arrays; 12 import java.util.list; 13 14 public class streamutils implements serializable{ 15 private static final long serialversionuid = 1l; 16 /** 17 * 序列化,list 18 * @param list 19 * @param file 20 * @param <t> 21 * @return 22 */ 23 public static <t> boolean writeobject(list<t> list,file file) 24 { 25 t[] array = (t[]) list.toarray(); 26 try(objectoutputstream out = new objectoutputstream(new fileoutputstream(file))) 27 { 28 out.writeobject(array); 29 out.flush(); 30 return true; 31 } 32 catch (ioexception e) 33 { 34 e.printstacktrace(); 35 return false; 36 } 37 } 38 39 /** 40 * 反序列化,list 41 * @param file 42 * @param <e> 43 * @return 44 */ 45 public static <e> list<e> readobjectforlist(file file) 46 { 47 e[] object; 48 try(objectinputstream out = new objectinputstream(new fileinputstream(file))) 49 { 50 object = (e[]) out.readobject(); 51 return arrays.aslist(object); 52 } 53 catch (ioexception e) 54 { 55 e.printstacktrace(); 56 } 57 catch (classnotfoundexception e) 58 { 59 e.printstacktrace(); 60 } 61 return null; 62 } 63 64 }
测试缓存结果:
1 package com.xinyan.springcloud.tjt; 2 3 import java.io.file; 4 import java.util.arraylist; 5 import java.util.list; 6 7 import lombok.extern.log4j.log4j; 8 9 @log4j 10 public class teststreamutils { 11 12 private final static list<string> listsave = new arraylist<>(); 13 private final static string file_cache = "file-tjt"; 14 15 public static void main(string[] args) throws exception { 16 teststreamutils.savelisttojvm("admin"); 17 teststreamutils.savelisttojvm("user"); 18 teststreamutils.savelisttojvm("lieiwantto"); 19 object listfromjvm = teststreamutils.getlistfromjvm(); 20 system.out.println("listfromjvm:"+listfromjvm); 21 } 22 23 24 public static void savelisttojvm(string input) throws exception{ 25 list<string> listinput = new arraylist<>(); 26 listsave.add(input); 27 listinput.addall(listsave); 28 boolean writeobject = streamutils.writeobject(listinput, new file(file_cache)); 29 if (!writeobject) { 30 log.info("savelisttojvm failed!"); 31 } 32 } 33 34 public static object getlistfromjvm() { 35 file file = new file(file_cache); 36 list<object> readobjectforlist = null; 37 if (file.exists()) { 38 readobjectforlist = streamutils.readobjectforlist(file); 39 if (readobjectforlist.size() >20) { 40 readobjectforlist.remove(0); 41 } 42 }else { 43 log.info("file not exist!"); 44 } 45 return readobjectforlist; 46 } 47 48 }
测试结果如下图:
上一篇: MySQL 主从复制原理及配置教程
下一篇: python —— 线程