• 缓存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 }

    测试结果如下图:

  • 相关阅读:
    基于KNN的newsgroup 18828文本分类器的Python实现
    基于Bayes和KNN的newsgroup 18828文本分类器的Python实现
    C++笔试题
    一号店笔试题
    最长递增子序列
    雅虎2015校招--研究工程师
    百度2013校园招聘笔试题(答案整理) – 机器学习/数据挖掘工程师
    windows下Python shell代码自动补全
    windows下scrapy安装
    实验四
  • 原文地址:https://www.cnblogs.com/taojietaoge/p/10852612.html
Copyright © 2020-2023  润新知