Giter Site home page Giter Site logo

laravel-cache's Introduction

webman-tech/laravel-cache

Laravel illuminate/cache for webman

介绍

站在巨人(laravel)的肩膀上使缓存使用更加可靠便捷

所有方法和配置与 laravel 几乎一模一样,因此使用方式完全参考 Laravel文档 即可

同步支持 RateLimiter

安装

composer require webman-tech/laravel-cache

使用

所有 API 同 laravel,以下仅对有些特殊的操作做说明

Facade 入口

使用 WebmanTech\LaravelCache\Facades\Cache 代替 Illuminate\Support\Facades\Cache

使用 WebmanTech\LaravelCache\Facades\RateLimiter 代替 Illuminate\Support\Facades\RateLimiter

command 支持

  • php webman cache:forget xxx: 删除缓存下的某个键

  • php webman cache:clear: 清空所有缓存 (!!注意:此方法使用 Cache::flush 来清除,影响范围见下文中的使用注意事项!!)

extend 支持

config/plugin/webman-tech/laravel-cache/cache.php 中配置 extend

<?php

return [
    'extend' => function(\Illuminate\Cache\CacheManager $cache) {
        $cache->extend('mongo', function () use ($cache) {
           return $cache->repository(new MongoStore);
        });
    }
];

PSR6 和 PSR16 支持

  • PSR16: Cache::psr16()

  • PSR6: Cache::psr6(),需要先安装依赖 symfony/cache

Throttle Middleware 支持

该库实现了类似 Laravel Route 下的 throttle(Middleware\ThrottleRequests), 适用于 webman 路由,用于快速处理接口限流等

配置

config/plugin/webman-tech/laravel-cache/rate_limiter.php 下配置 for, 配置方式同 Laravel 的 RateLimiter::for

使用

<?php
use Webman\Route;

Route::get('/example', function () {
    //
})->middleware([
    \WebmanTech\LaravelCache\Middleware\ThrottleRequestsFactory::class,
]);

Route::get('/example2', function () {
    //
})->middleware([
    new \WebmanTech\LaravelCache\Middleware\ThrottleRequestsFactory([
        'limiter_for' => 'upload', // 需要在 rate_limiter.php 中配置 upload 的 for
    ]),
]);

使用注意事项

关于默认的缓存过期时间

Laravel Cache 没有缓存的默认过期时间

Cache::put 方法的第三个参数 ttl,不传时为永久缓存,为 0 或负数时表示移除该缓存(等同于 forget)

Cache::flush 清楚的范围

会清空该存储器下的所有数据,而非指定的 prefix 下的,所以当缓存共享,通过 prefix 区分时,需要谨慎调用该方法

laravel-cache's People

Contributors

krissss avatar pxianyu 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.