2017 © Pedro Peláez
 

library laravel-local-cache

Cache remote files locally in Laravel

image

offline/laravel-local-cache

Cache remote files locally in Laravel

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Local File Cache for Laravel 5

[Build Status](https://travis-ci .org/OFFLINE-GmbH/laravel-local-cache), (*1)

This package allows you to cache remote files in the local filesystem. This is useful if you try to reduce bandwidth consumption when requesting files from services like Amazon S3., (*2)

The implementation also enables you to serve files from cache that may not be available at their remote location at times., (*3)

Install it

To install this package include it in your composer.json and run composer update:, (*4)

"require": {
   "offline/laravel-local-cache": "~1.0"
}

Add the Service Provider to the provider array in your config/app.php, (*5)

'Offline\LocalCache\LocalCacheServiceProvider'

Add an alias for the facade to your config/app.php, (*6)

'LocalCache' => 'Offline\LocalCache\Facades\LocalCache',

Publish the config:, (*7)

$ php artisan vendor:publish --provider="Offline\LocalCache\LocalCacheServiceProvider"

Create the directory storage/localcache (edit the storage_path setting in config/localcache.php to change this location)., (*8)

Use it

To cache a file use the getCachedHtml method. The file will be downloaded and stored to disk. The method returns the local URL for your file., (*9)

$string = 'http://www.offlinegmbh.ch/file.jpg';

// returns http://yoursite/cache/{hash}
var_dump(LocalCache::getCachedHtml($string));

By default, a /cache/{hash} route is generated which serves the file's contents with the correct mime type. To change the route, edit the route setting in config/localcache.php., (*10)

The getCachedHtml method works with any string that contains any number of URLs. It extracts and replaces the links accordingly., (*11)

$string = '<p>http://www.offlinegmbh.ch/file.jpg</p><p>http://www.offlinegmbh.ch/file2.jpg</p>';

// <p>http://yoursite/cache/{hash1}</p><p>http://yoursite/cache/{hash2}</p>
var_dump(LocalCache::getCachedHtml($string));

To prevent URLs from being cached, prefix them with a @ symbol:, (*12)

$string = '<p>@http://dont-cache-me/file.jpg</p>';
// <p>http://dont-cache-me/file.jpg</p>
var_dump(LocalCache::getCachedHtml($string));

Example Middleware

This example middleware caches all external files referenced in your template and replaces the URLs., (*13)

<?php

namespace App\Http\Middleware;

use Closure;
use Offline\LocalCache\Facades\LocalCache;

class LocalCacheMiddleware
{

    /**
     * Cache and replace links to external assets.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $response->setContent(
            LocalCache::getCachedHtml($response->getContent())
        );

        return $response;
    }

}

Add it to your app/Http/Kernel.php, (*14)

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'localcache'   => \App\Http\Middleware\LocalCacheMiddleware::class,
];

And use it in your routes.php or controller., (*15)

Route::get('/', [
    'uses'       => 'PageController@index',
    'middleware' => ['localcache']
]);

The Versions

11/03 2016

dev-master

9999999-dev

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

11/03 2016

v1.0.8

1.0.8.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

11/03 2016

dev-develop

dev-develop

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

16/02 2016

v1.0.7

1.0.7.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

21/09 2015

1.0.6

1.0.6.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

19/08 2015

1.0.5

1.0.5.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

18/08 2015

1.0.4

1.0.4.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

10/07 2015

1.0.3

1.0.3.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

17/06 2015

v1.0.2

1.0.2.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

17/06 2015

v1.0.1

1.0.1.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig

16/06 2015

v1.0.0

1.0.0.0

Cache remote files locally in Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tobias Kündig