<repo_name>prakashio/spring-cloud-cache<file_sep>/src/main/java/com/programmingarea/springcloudcache/repositories/UserRepository.java
package com.programmingarea.springcloudcache.repositories;

import com.programmingarea.springcloudcache.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByName(String name);
}
<file_sep>/src/main/java/com/programmingarea/springcloudcache/resource/UserResource.java
package com.programmingarea.springcloudcache.resource;

import com.programmingarea.springcloudcache.cache.UserCache;
import com.programmingarea.springcloudcache.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserResource {

    @Autowired
    UserCache userCache;

    @GetMapping("users/{name}")
    public List<User> findUsersByName(@PathVariable String name) {
        return userCache.findByName(name);
    }

}
<file_sep>/src/main/resources/application.properties
spring.cache.jcache.config=encache.xml

<file_sep>/src/main/java/com/programmingarea/springcloudcache/cache/UserCache.java
package com.programmingarea.springcloudcache.cache;

import com.programmingarea.springcloudcache.model.User;
import com.programmingarea.springcloudcache.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserCache {

    @Autowired
    UserRepository userRepository;

    @Cacheable(value = "usersCache", key="#name")
    public List<User> findByName(String name) {
        System.out.println("Retriving from the database for name:"+ name);
        return userRepository.findByName(name);
    }
}
<file_sep>/src/main/java/com/programmingarea/springcloudcache/loader/loader.java
package com.programmingarea.springcloudcache.loader;

import com.programmingarea.springcloudcache.model.User;
import com.programmingarea.springcloudcache.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class loader {

    @Autowired
    UserRepository usersRepository;

    @PostConstruct
    public void load() {
        List<User> usersList = getList();
        usersRepository.saveAll(usersList);
    }

    public List<User> getList() {
        List<User> usersList = new ArrayList<>();
        usersList.add(new User("Paras", "Developer", "1"));
        usersList.add(new User("Arun", "Deployment", "2"));
        usersList.add(new User("Surya", "UI", "2"));
        return usersList;

    }
}