در حال بارگذاری ...

Introducing cache Helper0:00

Okay, let's move on to the cache global function in Illuminate\Foundation\Helpers. So if we scroll down, you'll see that there's a brand new cache function. All of this craziness written by yours truly, actually. Pretty funky stuff, but it gives you a lot of flexibility in terms of the function signature. So let me show you. Once again, in Laravel 5.3, php artisan tinker. If I run cache, you'll see that it's a function, of course, that by default returns an instance of CacheManager. But if I switch over to Laravel 5.2 and run php artisan tinker, yeah, of course, the cache function did not exist prior to this new release. Okay, so how does it work? Well, it's basically very similar to the session. So for example, you may know that, in fact, let me just show you. All right, so notice that the session function, well, you can do some funky stuff here. Like if you don't call session, it returns an instance of the SessionManager. Or

Session Helper Behavior0:47

let me just show you. All right, so notice that the session function, well, you can do some funky stuff here. Like if you don't call session, it returns an instance of the SessionManager. Or if you provide an array, then it's going to put to the session. So you could say session foo is bar, and now we have put a new item into the session. Further, if we run session and provide a string, it's going to fetch from the session. Or you can provide a default that will be returned in its place. So yeah, kind of cool stuff. A lot of people don't like these really dynamic functions, but I think it can be kind of cool when used thoughtfully. Anyways, for cache, exact same thing. So if we take a look at cache, you can see that if we have no arguments, then once again, we return the CacheManager, which means you can call any methods on it, just like if you did cache put or cache get, any of those things will be available. Anyways, next you can see if you

Getting and Setting Cache1:35

we return the cache manager, which means you can call any methods on it, just like if you did cache.put or cache.get, any of those things will be available. Anyways, next you can see if you provide a string as the first argument, we're going to fetch from the cache. And once again, alternatively use a default. So if we were to say give me this from the cache, but use a default, well, in that case, of course, we return defaults because this does not exist in the cache. To put to the cache, once again, you can use an array. So I could do like $user with an ID of one votes, you know, whatever it is you want to put there, maybe some IDs they voted on. Then you can provide an expiration date, which of course you need for the cache. If you don't provide one, it'll give you a little exception here to let you know to add it. So in this case, let's cache it for, I don't know, 60 minutes, and we're all set. So now give me all

Global Helper vs Injection2:18

If you don't provide one, it'll give you a little exception here to let you know to add it. So in this case, let's cache it for, I don't know, 60 minutes, and we're all set. So now give me all of the User with an ID of ones votes. Or once again, yeah, just use the cache function if you want to be explicit. So you could say cache put foo is bar for 60 minutes, and you're going to get the exact same thing. So cache get foo, or cache foo, or once again, cache foo equals bar, and then put that for 60 minutes. So it's just a matter of what feels good to you. You can use this if you want. If you're a big fan of the global functions, if you think it's the worst thing in the world, then of course you can declare, for example, cache, what is it, cache contracts repository. Yeah, you can type in that, and then you can have a nice decoupled way. It just depends upon how you want to build your applications. Okay, so that's yet another new

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟