---
title: "缓存管理-接口设计"
url: "https://goframe.org/docs/core/gcache-interface"
markdown_url: "https://goframe.org/docs/core/gcache-interface.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "核心组件"
category: "核心组件 / 缓存管理"
published: "2024-10-21"
updated: "2026-02-11"
description: "详细介绍`GoFrame`框架缓存管理组件的接口化设计。通过`Adapter`接口，开发者可以灵活地自定义和扩展缓存管理对象。任何实现了`Adapter`接口的对象都可注册到缓存管理中，实现不同缓存策略的无缝接入。讲解`SetAdapter`方法用于将接口实现应用到`Cache`对象，`GetAdapter`方法用于获取当前注册的接口实现。特别提示`SetAdapter`方法不是并发安全的，不应在多个协程中并发调用。"
keywords: ["GoFrame", "GoFrame框架", "缓存管理", "接口设计", "Adapter接口", "gcache", "缓存接口", "自定义实现", "接入缓存", "GoFrame缓存"]
---

# 缓存管理-接口设计

缓存组件采用了接口化设计，提供了`Adapter`接口，任何实现了`Adapter`接口的对象均可注册到缓存管理对象中，使得开发者可以对缓存管理对象进行灵活的自定义实现和扩展。

## 接口定义

`Adapter` 接口定义如下：

[https://pkg.go.dev/github.com/gogf/gf/v2/os/gcache#Adapter](https://pkg.go.dev/github.com/gogf/gf/v2/os/gcache#Adapter)

## 注册接口实现

通过该方法将实现的`adapter`应用到对应的`Cache`对象上：

```go
// SetAdapter changes the adapter for this cache.
// Be very note that, this setting function is not concurrent-safe, which means you should not call
// this setting function concurrently in multiple goroutines.
func (c *Cache) SetAdapter(adapter Adapter)
```

具体示例请参考 [缓存管理-Redis缓存](https://goframe.org/docs/core/gcache-redis) 章节。

## 获取接口实现

通过该方法获取当前注册的`adapter`接口实现对象上：

```go
// GetAdapter returns the adapter that is set in current Cache.
func (c *Cache) GetAdapter() Adapter
```

具体示例请参考 [缓存管理-Redis缓存](https://goframe.org/docs/core/gcache-redis) 章节。
