Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Spring Application Development

You're reading from   Mastering Spring Application Development Gain expertise in developing and caching your applications running on the JVM with Spring

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781783987320
Length 288 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Mankale Mankale
Author Profile Icon Mankale
Mankale
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Spring Mongo Integration 2. Messaging with Spring JMS FREE CHAPTER 3. Mailing with Spring Mail 4. Jobs with Spring Batch 5. Spring Integration with FTP 6. Spring Integration with HTTP 7. Spring with Hadoop 8. Spring with OSGI 9. Bootstrap your Application with Spring Boot 10. Spring Cache 11. Spring with Thymeleaf Integration 12. Spring with Web Service Integration Index

Implementing your own caching algorithm

In this section, let us start by implementing a simple cache algorithm and see its draw backs, and then show how spring caching can be used to solve the problems.

Let's draw a simple flow chart to look at the caching scenario:

Implementing your own caching algorithm

Let's see how we can implement caching in a simple way. Think of generating a Fibonacci number. A Fibonacci number is generated by adding its previous two Fibonacci numbers. So we can compute a simple class in java and see how we can use caching here.

Let's create a map to cache the objects:

import java.util.HashMap;
import java.util.Map;
public class FibonacciCache {
  private Map<Long, Long> cachemap = new HashMap<>();
  public FibonacciCache() {
    // The base case for the Fibonacci Sequence
    cachemap.put(0L, 1L);
    cachemap.put(1L, 1L);
  }
  public Long getNumber(long index) {
    // Check if value is in cache
    if (cachemap.containsKey(index)) {
     return cachemap.get(index);
    }

  ...
lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Mastering Spring Application Development
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon