[WIP] Native Cache

Introduction

This resource saves natives into a cache table reducing the overall number of server requests from the Client. This gives an all round boost to overall server performance and decreases the loading times of any scripts that use the cache.

For Example:

The esx_policejob client file calls the PlayerPedId() native 18 times - the same data can be retrieved from a single call through exports.cache:requestCache().PlayerPed

(Note: ESX is not a requirement for this script)

In a nutshell, it cuts the amount of native calls by a huge amount depending on what you cache.

This is an ongoing work in progress - so any contribution or input will always be greatly appreciated!

You can download the script here from github

Installation

Step 1 - Calling The Cache Data

Let’s say you want to return the PlayerPedId() - we can call it through an export from the script by using:

exports.cache:requestCache().PlayerPed

Make sure that the resource name doesn’t have a hyphen in the name (the - symbol) or it’ll return an error of:

attempt to index a nil value (global 'cache')

Step 2 - Replacing Native Calls With Cached Data

You’ll need to do this manually by yourself. Here’s an example of what you’ll need to do:

So here we havePlayerPedId() saved into the cache:

Cache.PlayerPed = PlayerPedId()

Anywhere you see PlayerPedId() in your script you’ll need to replace it with:

exports.cache:requestCache().PlayerPed

Step 3 - server.cfg

Place start mascotte-cache before your framework/scripts

Adding More Natives To The Cache

I’ll update this as regularly as I can, but if you wish to add your own cached values then all you need to do is follow this format:

Cache.YourNameHere = NativeYouWantToCache() – Add into the thread

Changelog

  • Cache is no longer called through the global environment
  • Name was changed to just cache to prevent an earlier mentioned error from occuring
  • Forum thread name was changed to better represent any contributors to the project
  • Script was given a huge clean up

Kudos

  • @d0p3t - As the only information I could find about creating a cache to begin with was from a post he made on the CFX discord explaining how to cache data from natives

  • @NotSomething - For his contribution towards cleaning up a lot of the code and who I consulted a lot with during the commencment of development of the script

14 Likes

Interesting and cool feature.

Great post, very informational, deff will give this a shot! Thanks!

This is amazing! Nice release bro.

This will helping a lot of people who has performance problems on their servers.

What is WIP?

It means Work In Progress

what do i have to do to every script in order make this native cache script work?

start by reading his main post

Thanks for the positive feedback folks! This time last year I knew nothing about fivem or LUA and in the space of a year I’ve learned alot - mostly because I can study the many free scripts that exist, so I’m always happy to share anything that may help anyone with whatever and with learning :slight_smile:

1 Like

How would this compare in a real script example?

It speaks for itself - as I said at the start of this post, esx_policejob calls PlayerPedId() 18 times but while using the cache its called once. Thats just one script, so imagine how much of a performance of a boost you’ll notice

Yes that will obviously improve performance but I don’t think that’s even noticeable.
I don’t run a 2003 pentium, none of us do if anything that will maybe give you 5 fps max?

FYI performance is more than just your FPS :wink: I can’t imagine it giving much of an FPS boost - as you said, probably an increase of 5 max. The point of caching natives is it’ll speed up your loading times for scripts and it should give some reduction in use to your servers hardware.

When I finish converting all the scripts on my dev server to use the cache - I’ll share the results of how the cached server works compared to the non cached version

My full FiveM server uses around 1/5 of my 4GB RAM and ~10% CPU and uses around 400 ESX resources, this isn’t useful tbh

Just because you don’t understand why it’s useful doesn’t mean it’s not useful… Caching natives is actually very efficient.

Besides, it’s hard to take you serious now after you just stating you run over 400 resources…

I will post stats when they become available so you can actually see yourself then how efficient it is

6 Likes

Nice job, I will use this definitely.

I run 400 resources for testing purposes I do not own a public server, as to “you don’t understand how it works” I am in University taking computer science, know assembly and learning prolog now, there’s no way I can not understand how a CPU and memory works.

Suggestion:

As pointed out by @iSentrie you probably shouldn’t cache or use the GetPlayerPed(-1) native as the same data can be found from PlayerPedId() . So if it’s efficiency we’re after, then it’s more efficient to only ever have to call one native instead of two.

Can i use this with like streaming files like mlos ? :slight_smile:

PlayerPedId() *****

1 Like