Giter Club home page Giter Club logo

sivan_cache's Introduction

SivanCache渐进式缓存框架😀

这个项目有logo啦!

SIVANCACHE Logo

项目介绍😇

Java实现渐进式 kv缓存框架, 为日常开发提供一套简单易用的缓存框架 ,便于后期多级缓存开发。

截至目前已经开发的功能如下:

● 支持 expire 过期特性,实现了定时删除和惰性删除两种过期键删除策略;【√】

● 内置LRU、LRU-2、 LFU、2Q 等多种缓存淘汰算法;【√】

● 自定义删除监听器和慢操作监听器,实现了对删除和慢操作的监听;【√】

● 支持 AOF 持久化机制,实现了对AOF文件的载入与数据还原;【√】

● 自定义渐进式 rehash HashMap,实现了渐进式 rehash 扩容机制。【√】

项目设计思路

考虑点主要在数据用何种方式存储,能存储多少数据,多余的数据如何处理以及是否支持持久化等几个点。

  • 数据结构

    • 本地缓存最常见的是直接使用 Map 来存储,比如 guava 使用 ConcurrentHashMap,ehcache 也是用了 ConcurrentHashMap,Mybatis 二级缓存使用 HashMap 来存储:
    • 或者复杂的如 redis 一样提供了多种数据类型哈希,列表,集合,有序集合等,底层使用了双端链表,压缩列表,集合,跳跃表等数据结构;
  • 存贮上限

    • 因为是本地缓存,内存有上限,所以一般都会指定缓存对象的数量比如 1024,当达到某个上限后需要有某种淘汰策略去删除多余的数据;
  • 淘汰策略

    • 配合存贮上限之后使用,常见的比如有FIFO(先进先出)、 LRU(最近最少使用)、LFU(最近最不常用)、SOFT(软引用)、 LRU-K 、2Q 等策略;
  • 过期时间

    • 设置过期时间,让缓存数据在指定时间过后自动删除;常见的过期数据删除策略有两种方式:定时删除和惰性删除;
      • 定时删除:设置某个key的过期时间同时,我们创建一个定时器,让定时器在该过期时间到来时,立即执行对其进行删除的操作。
      • 惰性删除:设置该key过期时间后,我们不去管它,当需要该key时,我们在检查其是否过期,如果过期,我们就删掉它,反之返回该key。
  • 线程安全

    • 像 redis 是直接使用单线程处理,所以就不存在线程安全问题;而我们现在提供的本地缓存往往是可以多个线程同时访问的,所以线程安全是不容忽视的问题;并且线程安全问题是不应该抛给使用者去保证;
    • 尽量用线程安全的类去存储数据,比如使用 ConcurrentHashMap 代替 HashMap;或者提供相应的同步处理类,比如 Mybatis 提供了 SynchronizedCache:
  • 简明的接口

    • 提供常用的 get,put,remove,clear,getSize 方法即可。
  • 是否支持持久化

    • 如果我们只是把文件放在内存中,应用重启信息就丢失了。有时候我们希望这些 key/value 信息可以持久化,存储到文件或者 database 中。
    • 持久化的好处是重启之后可以再次加载文件中的数据,这样就起到类似热加载的功效;
    • 像Redis提供了两种不同的持久化方法可以将数据存储在磁盘中,一种是快照RDB,另一种是只追加文件AOF
  • 监听器

    • 根据实际工作体验,我们可以添加对删除和慢日志的监听,然后有对应的存储或者报警,这样更加方便问题的分析和快速反馈。

TODO

  • 优化过期特性,看看能不能全部仿Redis;
  • 引入AOF + RDB双持久化策略;使用了混合持久化;
    • AOF 文件的前半部分是 RDB 格式的全量数据,后半部分是 AOF 格式的增量数据。重启 Cache 加载数据的时候,由于前半部分是 RDB 内容,这样加载的时候速度会很快。
  • 调用GPT3.5接口,实现AI生成功能。

写在最后

欢迎你来做一些贡献!如果你有更好的想法,可以联系我!

联系我们

邮箱:[email protected]

sivan_cache's People

Contributors

xiaoxinxing66 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.