| package com.example.google.guava.demo.cache; |
| |
| import com.example.google.guava.demo.model.Company; |
| import com.example.google.guava.demo.model.Department; |
| import com.example.google.guava.demo.model.Employee; |
| import com.google.common.cache.Cache; |
| import com.google.common.cache.CacheBuilder; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.concurrent.Callable; |
| import java.util.concurrent.ExecutionException; |
| import java.util.concurrent.TimeUnit; |
| |
| |
| * <p> |
| * <code>CacheTest</code> |
| * </p> |
| * Description: 以员工表、部门表、公司表三张表的缓存为例 |
| * |
| * @author Mcchu |
| * @date 2017/10/20 9:34 |
| */ |
| public class CacheTest<K,V> { |
| |
| private Cache<K, V> cache= CacheBuilder |
| .newBuilder() |
| .maximumSize(2) |
| .expireAfterWrite(10, TimeUnit.MINUTES) |
| .build(); |
| |
| |
| @SuppressWarnings("unchecked") |
| private Object getCache(K key,final String threadName){ |
| Object value=null; |
| |
| try { |
| System.out.println("线程:"+threadName+"获取数据"); |
| |
| |
| if ( "company".equals(key) ){ |
| Callable callable = ()->{ |
| System.out.println("###获取公司缓存数据###"); |
| return (List<Company>) getCompanyCacheList(); |
| }; |
| value = cache.get(key,callable); |
| } |
| if ( "department".equals(key) ){ |
| Callable callable = ()->{ |
| System.out.println("###获取部门缓存数据###"); |
| return (List<Department>) getDepartmentCacheList(); |
| }; |
| value = cache.get(key,callable); |
| } |
| if ( "employee".equals(key) ){ |
| Callable callable = ()->{ |
| System.out.println("###获取人员缓存数据###"); |
| return (List<Employee>) getEmployeeCacheList(); |
| }; |
| value = cache.get(key,callable); |
| } |
| |
| |
| |
| public V call () { |
| System.out.println ("### 获取缓存数据 ###"); |
| return (V) "dataValue"; |
| } |
| });*/ |
| } catch (ExecutionException e) { |
| e.printStackTrace(); |
| } |
| |
| return value; |
| } |
| |
| |
| public static void main(String[] args) { |
| final CacheTest<String,String> cacheTest=new CacheTest<String,String>(); |
| |
| Runnable task1 = () -> { |
| System.out.println("线程t1开始"); |
| Object value=cacheTest.getCache("company","T1"); |
| System.out.println("线程t1获取到的缓存值:"+value); |
| System.out.println("线程t1开始"); |
| }; |
| new Thread(task1).start(); |
| |
| Runnable task2 = () -> { |
| System.out.println("线程t2开始"); |
| Object value=cacheTest.getCache("department","T2"); |
| System.out.println("线程t2获取到的缓存值:"+value); |
| System.out.println("线程t2结束"); |
| }; |
| new Thread(task2).start(); |
| |
| Runnable task3 = () -> { |
| System.out.println("线程t3开始"); |
| Object value=cacheTest.getCache("department","T3"); |
| System.out.println("线程t3获取到的缓存值:"+value); |
| System.out.println("线程t3结束"); |
| }; |
| new Thread(task3).start(); |
| |
| |
| |
| |
| @Override |
| public void run () { |
| System.out.println ("线程 t1 开始"); |
| Object value=cacheTest.getCache ("key1","T1"); |
| System.out.println ("线程 t1 获取到的缓存值:"+value); |
| System.out.println ("线程 t1 开始"); |
| } |
| }); |
| Thread t2=new Thread (new Runnable () { |
| @Override |
| public void run () { |
| System.out.println ("线程 t2 开始"); |
| Object value=cacheTest.getCache ("key1","T2"); |
| System.out.println ("线程 t2 获取到的缓存值:"+value); |
| System.out.println ("线程 t2 结束"); |
| } |
| }); |
| Thread t3=new Thread (new Runnable () { |
| @Override |
| public void run () { |
| System.out.println ("线程 t3 开始"); |
| Object value=cacheTest.getCache ("key3","T3"); |
| System.out.println ("线程 t3 获取到的缓存值:"+value); |
| System.out.println ("线程 t3 结束"); |
| } |
| }); |
| t1.start (); |
| t2.start (); |
| t3.start ();*/ |
| } |
| |
| |
| * 员工假数据(实际来源可能是数据库或远程接口) |
| * @return 人员列表 |
| */ |
| private static List<Employee> getEmployeeCacheList(){ |
| List<Employee> employeeList = new ArrayList<>(); |
| Employee employee1 = new Employee("Jalen","102","20171121"); |
| Employee employee2 = new Employee("Jermy","101","20171122"); |
| Employee employee3 = new Employee("Abely","104","20171123"); |
| Employee employee4 = new Employee("Mercy","103","20171124"); |
| employeeList.add(employee1); |
| employeeList.add(employee2); |
| employeeList.add(employee3); |
| employeeList.add(employee4); |
| return employeeList; |
| } |
| |
| |
| * 部门假数据(实际来源可能是数据库或远程接口) |
| * @return 部门列表 |
| */ |
| private static List<Department> getDepartmentCacheList(){ |
| List<Department> departmentList = new ArrayList<>(); |
| Department department1 = new Department("101","采购部","1"); |
| Department department2 = new Department("102","财务部","1"); |
| Department department3 = new Department("103","信管部","1"); |
| Department department4 = new Department("104","外贸部","1"); |
| departmentList.add(department1); |
| departmentList.add(department2); |
| departmentList.add(department3); |
| departmentList.add(department4); |
| return departmentList; |
| } |
| |
| |
| * 公司假数据(实际来源可能是数据库或远程接口) |
| * @return 公司列表 |
| */ |
| private static List<Company> getCompanyCacheList(){ |
| List<Company> companyList = new ArrayList<>(); |
| Company company1 = new Company("1","Sanmina"); |
| Company company2 = new Company("2","Google"); |
| Company company3 = new Company("3","Tecent"); |
| companyList.add(company1); |
| companyList.add(company2); |
| companyList.add(company3); |
| return companyList; |
| } |
| } |