Cache Flashcards

1
Q

What are the cache drivers supported by Laravel?

A

Memcached, Redis & file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What the default cache driver used by Laravel?

A

The file cache driver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the three supported cache drivers are recommended for larger apps?

A

Memcached or Redis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In order to use the file/database cache driver what is required to be setup?

A

When using the database cache driver, you will need to setup a table to contain the cache items. You’ll find an example Schema declaration for the table below:

Schema::create('cache', function ($table) {
    $table->string('key')->unique();
    $table->text('value');
    $table->integer('expiration');
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which Artisan Command will generate a migration with the proper schema for the database cache driver?

A

php artisan cache:table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

For Memcached what is the name of the required package?

A

Memcached PECL package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In which file can you list all of your Memcached servers?

A

You may list all of your Memcached servers in the config/cache.php configuration file:

'memcached' => [
    [
        'host' => '127.0.0.1',
        'port' => 11211,
        'weight' => 100
    ],
],
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which option in the config/cache.php file can also be set to to a UNIX socket path?

A

You may also set the host option to a UNIX socket path. If you do this, the port option should be set to 0:

'memcached' => [
    [
        'host' => '/var/run/memcached/memcached.sock',
        'port' => 0,
        'weight' => 100
    ],
],
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Before using a Redis cache with Laravel, you will need to either install?

A

the predis/predis package (~1.0) via Composer or install the PhpRedis PHP extension via PECL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What contracts provide access to Laravel’s cache services?

A

Illuminate\Contracts\Cache\Factory and Illuminate\Contracts\Cache\Repository

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of the factory contract?

A

The Factory contract provides access to all cache drivers defined for your application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the repository contract?

A

The Repository contract is typically an implementation of the default cache driver for your application as specified by your cache configuration file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the Cache Facade?

A

The Cache facade provides convenient, terse access to the underlying implementations of the Laravel cache contracts:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How are you able to access multiple cache stores?

A

Using the Cache facade, you may access various cache stores via the store method. The key passed to the store method should correspond to one of the stores listed in the stores configuration array in your cache configuration file:

$value = Cache::store(‘file’)->get(‘foo’);

Cache::store(‘redis’)->put(‘bar’, ‘baz’, 10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the get method?

A

The get method on the Cache facade is used to retrieve items from the cache. If the item does not exist in the cache, null will be returned. If you wish, you may pass a second argument to the get method specifying the default value you wish to be returned if the item doesn’t exist:

$value = Cache::get(‘key’);

$value = Cache::get(‘key’, ‘default’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the get method closure?

A

You may even pass a Closure as the default value. The result of the Closure will be returned if the specified item does not exist in the cache. Passing a Closure allows you to defer the retrieval of default values from a database or other external service:

$value = Cache::get(‘key’, function () {
return DB::table(…)->get();
});

17
Q

What is the purpose of the has method?

A

The has method may be used to determine if an item exists in the cache. This method will return false if the value is null or false:

if (Cache::has('key')) {
    //
}
18
Q

What is the purpose of the increment and decrement methods?

A

The increment and decrement methods may be used to adjust the value of integer items in the cache. Both of these methods accept an optional second argument indicating the amount by which to increment or decrement the item’s value:

Cache::increment(‘key’);
Cache::increment(‘key’, $amount);
Cache::decrement(‘key’);
Cache::decrement(‘key’, $amount);

19
Q

What is the purpose of the remember method?

A

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn’t exist. For example, you may wish to retrieve all users from the cache or, if they don’t exist, retrieve them from the database and add them to the cache. You may do this using the Cache::remember method:

$value = Cache::remember(‘users’, $minutes, function () {
return DB::table(‘users’)->get();
});

If the item does not exist in the cache, the Closure passed to the remember method will be executed and its result will be placed in the cache.

20
Q

What is the purpose of the pull method?

A

If you need to retrieve an item from the cache and then delete the item, you may use the pull method. Like the get method, null will be returned if the item does not exist in the cache:

$value = Cache::pull(‘key’);

21
Q

What is the purpose of the put method?

A

You may use the put method on the Cache facade to store items in the cache. When you place an item in the cache, you need to specify the number of minutes for which the value should be cached:

Cache::put(‘key’, ‘value’, $minutes);
Instead of passing the number of minutes as an integer, you may also pass a DateTime instance representing the expiration time of the cached item:

$expiresAt = Carbon::now()->addMinutes(10);

Cache::put(‘key’, ‘value’, $expiresAt);

22
Q

What is the purpose of the add method?

A

The add method will only add the item to the cache if it does not already exist in the cache store. The method will return true if the item is actually added to the cache. Otherwise, the method will return false:

Cache::add(‘key’, ‘value’, $minutes);

23
Q

What is the purpose of the forever method?

A

The forever method may be used to store an item in the cache permanently. Since these items will not expire, they must be manually removed from the cache using the forget method:

Cache::forever(‘key’, ‘value’);

24
Q

What is the purpose of the forget method?

A

You may remove items from the cache using the forget method:

Cache::forget(‘key’);

25
Q

What is the purpose of the flush method?

A

You may clear the entire cache using the flush method:

Cache::flush();

26
Q

In addition to using the Cache facade or cache contract, what else can be used to retrieve and store data via the cache?

A

In addition to using the Cache facade or cache contract, you may also use the global cache function to retrieve and store data via the cache. When the cache function is called with a single, string argument, it will return the value of the given key:

$value = cache(‘key’);

If you provide an array of key / value pairs and an expiration time to the function, it will store values in the cache for the specified duration:

cache([‘key’ => ‘value’], $minutes);

cache([‘key’ => ‘value’], Carbon::now()->addSeconds(10));

27
Q

Which cache driver(s) don’t support cache tags?

A

Cache tags are not supported when using the file or database cache drivers.

28
Q

How do you tag related items in the cache?

A

Cache tags allow you to tag related items in the cache and then flush all cached values that have been assigned a given tag. You may access a tagged cache by passing in an ordered array of tag names. For example, let’s access a tagged cache and put value in the cache:

Cache::tags([‘people’, ‘artists’])->put(‘John’, $john, $minutes);

Cache::tags([‘people’, ‘authors’])->put(‘Anne’, $anne, $minutes);

29
Q

How do you access tagged cache items?

A

To retrieve a tagged cache item, pass the same ordered list of tags to the tags method and then call the get method with the key you wish to retrieve:

$john = Cache::tags([‘people’, ‘artists’])->get(‘John’);

$anne = Cache::tags([‘people’, ‘authors’])->get(‘Anne’);

30
Q

How do you remove tagged cache items?

A

You may flush all items that are assigned a tag or list of tags. For example, this statement would remove all caches tagged with either people, authors, or both. So, both Anne and John would be removed from the cache:

Cache::tags([‘people’, ‘authors’])->flush();
In contrast, this statement would remove only caches tagged with authors, so Anne would be removed, but not John:

Cache::tags(‘authors’)->flush();

31
Q

What cache events are you able to listen for? And where would you store your listeners?

A

Typically, you should place these event listeners within your EventServiceProvider:

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Illuminate\Cache\Events\CacheHit' => [
        'App\Listeners\LogCacheHit',
    ],
'Illuminate\Cache\Events\CacheMissed' => [
    'App\Listeners\LogCacheMissed',
],

'Illuminate\Cache\Events\KeyForgotten' => [
    'App\Listeners\LogKeyForgotten',
],

'Illuminate\Cache\Events\KeyWritten' => [
    'App\Listeners\LogKeyWritten',
], ];