Nebulex 缓存工具库入门指南

Nebulex 缓存工具库入门指南

【免费下载链接】nebulex In-memory and distributed caching toolkit for Elixir. 【免费下载链接】nebulex 项目地址: https://gitcode.com/gh_mirrors/ne/nebulex

概述

Nebulex 是一个专为 Elixir 设计的本地和分布式缓存工具库。它的 API 设计灵感来源于 Ecto,继承了其简洁性、灵活性和可插拔架构的特点。开发者可以像使用 Ecto 适配器一样,为 Nebulex 提供自己的缓存实现。

环境准备

创建新项目

首先创建一个支持监督树的新 Elixir 项目:

mix new blog --sup

--sup 选项确保应用程序包含监督树结构,这是 Nebulex 运行所必需的。

添加依赖

mix.exs 文件中添加 Nebulex 及其可选依赖:

defp deps do
  [
    {:nebulex, "~> 2.6"},
    {:shards, "~> 1.0"},      # 当使用 :shards 作为后端时
    {:decorator, "~> 1.4"},   # 当使用缓存注解时
    {:telemetry, "~> 1.0"}    # 当使用 Telemetry 事件时
  ]
end

安装依赖:

mix deps.get

配置缓存

生成缓存模块

运行以下命令生成缓存配置:

mix nbx.gen.cache -c Blog.Cache

这会生成两个主要文件:

  1. config/config.exs 中的配置:
config :blog, Blog.Cache,
  backend: :shards,
  gc_interval: :timer.hours(12),
  max_size: 1_000_000,
  allocated_memory: 2_000_000_000,
  gc_cleanup_min_timeout: :timer.seconds(10),
  gc_cleanup_max_timeout: :timer.minutes(10)
  1. lib/blog/cache.ex 中的缓存模块:
defmodule Blog.Cache do
  use Nebulex.Cache,
    otp_app: :blog,
    adapter: Nebulex.Adapters.Local
end

启动缓存进程

lib/blog/application.ex 中将缓存添加到监督树:

def start(_type, _args) do
  children = [
    Blog.Cache
  ]
  # ...

重要提示:确保缓存进程在依赖它的进程之前启动,避免出现竞态条件。

基本操作

插入数据

  1. 插入单个条目:
user = %{id: 1, first_name: "Galileo", last_name: "Galilei"}
Blog.Cache.put(user[:id], user, ttl: :timer.hours(1))
  1. 批量插入:
users = %{
  1 => %{id: 1, first_name: "Galileo", last_name: "Galilei"},
  2 => %{id: 2, first_name: "Charles", last_name: "Darwin"}
}
Blog.Cache.put_all(users)

条件插入

  1. 仅当键不存在时插入:
Blog.Cache.put_new(new_user.id, new_user)
  1. 仅更新已存在的键:
Blog.Cache.replace(existing_user.id, existing_user)

查询数据

  1. 获取单个值:
Blog.Cache.get(1)
  1. 检查键是否存在:
Blog.Cache.has_key?(1)
  1. 批量获取:
Blog.Cache.get_all([1, 2, 3])

更新数据

Blog.Cache.get_and_update(1, fn v ->
  if v, do: {v, %{v | first_name: "X"}}, else: {v, initial}
end)

计数器操作

Blog.Cache.incr(:my_counter)      # 默认+1
Blog.Cache.incr(:my_counter, 5)   # 自定义增量
Blog.Cache.incr(:my_counter, -5)  # 减量

删除数据

  1. 简单删除:
Blog.Cache.delete(1)
  1. 获取并删除:
Blog.Cache.take(1)

高级功能

查询和流式处理

  1. 查询所有匹配项:
Blog.Cache.all()
  1. 使用匹配规范查询:
spec = [{{:_, :"$1", :"$2", :_, :_}, [{:>, :"$2", 10}], [{{:"$1", :"$2"}}]}]
Blog.Cache.all(spec)
  1. 流式处理:
Blog.Cache.stream(page_size: 100)

分区缓存

分区缓存通过 Nebulex.Adapters.Partitioned 适配器实现,适合大规模数据:

defmodule Blog.PartitionedCache do
  use Nebulex.Cache,
    otp_app: :blog,
    adapter: Nebulex.Adapters.Partitioned,
    primary_storage_adapter: Nebulex.Adapters.Local
end

多级缓存

多级缓存通过 Nebulex.Adapters.Multilevel 实现,通常包含 L1(本地)和 L2(分区)两级:

defmodule Blog.NearCache do
  use Nebulex.Cache,
    otp_app: :blog,
    adapter: Nebulex.Adapters.Multilevel

  defmodule L1 do
    use Nebulex.Cache,
      otp_app: :blog,
      adapter: Nebulex.Adapters.Local
  end

  defmodule L2 do
    use Nebulex.Cache,
      otp_app: :blog,
      adapter: Nebulex.Adapters.Partitioned
  end
end

最佳实践

  1. 根据数据量选择合适的缓存策略:小数据量使用本地缓存,大数据量考虑分区缓存
  2. 合理设置 TTL 避免内存泄漏
  3. 多级缓存中,L1 适合高频访问的小数据,L2 适合大数据量存储
  4. 监控缓存命中率和内存使用情况

通过本指南,您应该已经掌握了 Nebulex 的基本用法和核心概念。更多高级特性和定制化配置可以参考 Nebulex 的详细文档。

【免费下载链接】nebulex In-memory and distributed caching toolkit for Elixir. 【免费下载链接】nebulex 项目地址: https://gitcode.com/gh_mirrors/ne/nebulex

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值