Clearing the cache | laravel-responsecache-go | Spatie       

 SPATIE  

laravel-responsecache-go
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-responsecache-go](https://spatie.be/docs/laravel-responsecache-go/v1)  Basic-usage  Clearing the cache

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-responsecache-go/v1) 

  Clearing the cache    
- [ Introduction ](https://spatie.be/docs/laravel-responsecache-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-responsecache-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-responsecache-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-responsecache-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/laravel-responsecache-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-responsecache-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-responsecache-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-responsecache-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-responsecache-go/v1/about-us)

Basic usage
-----------

- [ Caching responses ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/caching-responses)
- [ Flexible caching (SWR) ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/flexible-caching)
- [ Preventing caching ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/preventing-caching)
- [ Clearing the cache ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/clearing-the-cache)
- [ Using tags ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/using-tags)
- [ Events ](https://spatie.be/docs/laravel-responsecache-go/v1/basic-usage/events)

Advanced usage
--------------

- [ Custom cache profiles ](https://spatie.be/docs/laravel-responsecache-go/v1/advanced-usage/custom-cache-profiles)
- [ Creating a replacer ](https://spatie.be/docs/laravel-responsecache-go/v1/advanced-usage/creating-a-replacer)
- [ Customizing the serializer ](https://spatie.be/docs/laravel-responsecache-go/v1/advanced-usage/customizing-the-serializer)
- [ Customizing the hasher ](https://spatie.be/docs/laravel-responsecache-go/v1/advanced-usage/customizing-the-hasher)
- [ Configuration ](https://spatie.be/docs/laravel-responsecache-go/v1/advanced-usage/configuration)

 Clearing the cache
==================

###  On this page 

1. [ Clearing the entire cache ](#content-clearing-the-entire-cache)
2. [ Forgetting specific URIs ](#content-forgetting-specific-uris)
3. [ Forgetting a selection of cached items ](#content-forgetting-a-selection-of-cached-items)
4. [ Using model events ](#content-using-model-events)
5. [ Clearing tagged content ](#content-clearing-tagged-content)

Clearing the entire cache
-----------------------------------------------------------------------------------------------------------------------------------

You can clear the entire response cache programmatically.

```
err := responsecache.From(ctx).Clear(ctx)
```

You can also clear it using the console command.

```
laravel responsecache:clear
```

Only response entries are invalidated. Unlike PHP's untagged whole-store flush, unrelated application cache values remain untouched. Old response bytes expire naturally.

Forgetting specific URIs
--------------------------------------------------------------------------------------------------------------------------------

You can forget guest GET URLs, relative to `APP_URL` or absolute.

```
err := responsecache.From(ctx).ForgetURLs(ctx, "/posts", "/about")
```

```
laravel responsecache:clear --url=/posts
```

These shortcuts select untagged guest GETs. Supply a request and the exact route tags for tagged entries, a user partition or a custom hasher.

Forgetting a selection of cached items
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Go's standard request builder replaces PHP's fluent selector. It can carry a method, parameters, headers, cookies and a remote address to the same hasher used by the middleware.

```
req, err := http.NewRequestWithContext(ctx, http.MethodPut,
    "https://example.com/posts?category=go", nil)
if err != nil {
    return err
}
req.Header.Set("Accept-Language", "nl")
req.AddCookie(&http.Cookie{Name: "theme", Value: "dark"})
req.RemoteAddr = "192.0.2.1:1234"

request := responsecache.WithSuffix(web.NewRequest(req).WithContext(ctx), "100")
err = responsecache.From(ctx).Forget(request, "posts")
```

`WithContext(ctx)` preserves the application's service scope after constructing the standalone request. The default hasher uses the host, path, normalized query, method and user suffix. A custom hasher may use the remaining request inputs. Tags must match the route's tag set, independent of order. Global tags are included automatically.

Using model events
--------------------------------------------------------------------------------------------------------------

Register a native Laravel listener for the model events that should clear the response cache. Pass the listener's context to `responsecache.From(ctx).Clear(ctx)`. The package has no models, migrations or jobs of its own to replace.

Clearing tagged content
-----------------------------------------------------------------------------------------------------------------------------

```
err := responsecache.From(ctx).Clear(ctx, "posts")
```

Passing several tags invalidates entries bearing any of them, matching Laravel's tag invalidation behavior. In-flight writes retain old generation keys and cannot bring invalidated responses back.

For a memory store, the command clears only its own process. Use a shared store in production, or restart a memory-cached server.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
