C# Cache / API

I have mvc / c# / razor app that using the following method / class and I found out our developer put this on Views/Shared/Layoutpage.
It looks like it is called this method everytime when the page is refreshed. As a result, it is super easy to max out the API usage.

My question is: What is the best practice to use cache in c# to reduce this usage? In general, the layoutpage will be loaded as usual.
Please show me how it works in codes/c#. Thanks

 public static string GetLatitiudeByIP(string ip)
        {
            string returnValue = "34.028623";
            XmlDocument foo = new XmlDocument();
            foo.Load(String.Format("http://api.ipstack.com/" + ip + "?access_key=7efaasdfasdfasdfadsf3829ad25d56ab30492c9c79d24f1&output=xml&legacy=1"));
            XmlNode latitudeNode = foo.SelectSingleNode("/result/latitude");
            if(latitudeNode != null)
            {
                returnValue = latitudeNode.InnerText;
            }
            return returnValue;
        }
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
IMO, why is this even in the view? I would move this logic to a service layer where you can share the code between disparate projects.

Next, what exactly do you want to cache? You'll need to figure out how long you want whatever that is cached (i.e. figure out a cache invalidation process). Keep in mind that the more you cache, the more memory you consume on your server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
can you suggest what we should do? I do not know what you mean by 'service layer'
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Extending the previous comment, caching is great so long as cache data avoids becoming stale...

In other words, your cached value becomes old + the actual data has changed.

For caching, you just have to ensure you expire your cache at a rate faster than actual API data changes, so you make API calls when required, to actually pull truly new data.

For GEO coordinates, likely this data will change very infrequently, so caching this data to reduce API calls will likely be good + have few problems with data differences between your cache + API you're calling.
ITsolutionWizardAuthor Commented:
can you send me some suggestions in code?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.