sevenecks/laravel-simple-cache

laravel Cache 门面的包装器,通过静态类方法处理缓存键和命名空间缓存

0.0.9 2018-01-23 20:33 UTC

This package is auto-updated.

Last update: 2024-09-13 10:25:57 UTC


README

通过 Composer

composer require sevenecks/laravel-simple-cache

使用方法

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SevenEcks\LaravelSimpleCache\SimpleCache;

class PageController extends Controller
{
    /**
     * Gets the contact page from cache or renders it and saves to cache 
     * before returning
     *
     * @return string view
     */
    public function contact()
    {
        $view_name = 'contact';
        // check if we have a cached view
        if (!($view_content = SimpleCache::getCached($view_name))) {
            $view_content = view($view_name);
            // now let's cache our new view
            SimpleCache::setCache($view_name, $view_content->render());
        }
        // return the view either from cache or the newly created one
        return $view_content;
    }
}

Laravel 中缓存 CSRF 令牌

在某些情况下,您页面上可能有需要 CSRF 令牌的表单。如果您缓存包含 CSRF 令牌的页面,则该表单将无法正常工作(或仅您自己可以使用)。有一种解决方案,它不涉及部分页面缓存。

不要缓存实际的 CSRF 令牌。先缓存一个占位符,然后在您渲染视图内容或从缓存中拉取已渲染的内容后替换它。

例如,如果您有一个如下所示的 Blade 模板

<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">

您将用一些占位符文本替换 CSRF 令牌。

<!-- CSRF Token -->
<meta name="csrf-token" content="xxxxxxxxxxxxxx">

接下来,您将更新您的缓存代码,如下所示

public function contact()
{
    $view_name = 'contact';
    // check if we have a cached view
    if (!($view_content = SimpleCache::getCached($view_name))) {
        $view_content = view($view_name);
        // now let's cache our new view
        SimpleCache::setCache($view_name, $view_content->render());
    }
    // add in the csrf_token() post render/cache
    $view_content = str_replace('xxxxxxxxxxxxxx', csrf_token(), $view_content);
    // return the view either from cache or the newly created one
    return $view_content;
}

API

 // prefix for the cache key to namespace it
public static $cache_key_prefix = 'simple-cache-';

// text to tag the cache with by default
public static $cache_tag = '<!-- cache -->';

/**
 * Returns cached item by key if caching is enabled. If $tag_cached_content is
 * true then the the $cache_tag will be applied to the end of the content after 
 * it is pulled from the cache.
 *
 * @param  string $cache_key
 * @return mixed false if cache is disabled or not present, string if it exists.
 */
public static function getCached(string $cache_key, bool $tag_cached_content = true)

/**
 * Concatinates the prefix plus dash with suffix to create the cache key
 * namespace.
 *
 * @param  string $prefix
 * @param  string $suffix
 * @return string
 */
public static function buildCacheKey(string $prefix, string $suffix = '')

/**
 * Overwrite the static cache key prefix string with a user
 * provided string for customization purporses.
 *
 * @param string $new_prefix
 * @return none
 */
public static function setCacheKeyPrefix(string $new_prefix)

/**
 * Get the current $cache_key_prefix variable
 *
 * @return string
 */
public static function getCacheKeyPrefix()

/**
 * Overwrite the static cache tag string with a user
 * provided string for customization purposes.
 *
 * @param string $new_tag
 * @return none
 */
public static function setCacheTag(string $new_tag)

/**
 * Get the current $cache_tag static varible
 *
 * @return string
 */
public static function getCacheTag()

/**
 * Sets the cached content. $minutes_to_live set to -1 will live forever.
 *
 * @param  string $cache_key
 * @param  string $content
 * @param  integer $minutes_to_live = -1
 * @return mixed
 */
public static function setCache(string $cache_key, string $content, int $minutes_to_live = -1)

/**
 * Clear the entire cache
 *
 * @return none
 */
public static function clearCache()

现在您的应用程序应该能够正常工作,同时保持缓存的优点。

变更日志

请参阅变更日志以获取更多信息。

许可

MIT 许可证 (MIT)。请参阅许可文件以获取更多信息。