Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

• Senior Solutions Architect / Hazelcast • Завсегдатай конференций • Автор O’Reilly’s «Enterprise Web Development» Viktor Gamov @gamussa

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

public interface Cache { void put(K key, V value); V putIfAbsent(K key, V value); V get(K key); long size(); }

Slide 8

Slide 8 text

public class LocalCache implements Cache { ConcurrentMap storage = new ConcurrentHashMap<>(); @Override public void put(Integer key, Company value) { this.storage.put(key, value); } ... }

Slide 9

Slide 9 text

public class LocalCache implements Cache { ConcurrentMap storage = new ConcurrentHashMap<>(); @Override public void put(Integer key, Company value) { this.storage.put(key, value); } ... }

Slide 10

Slide 10 text

LocalCache myCache = new LocalCache<>(); // 20 MB String value= new String(new char[10000000]); Runtime runtime = Runtime.getRuntime(); int keyCount = 0; int mb = 1024 * 1024; while (true) { myCache.put(keyCount, value); keyCount++; System.out.printf("Unique Puts = %s keyCount : Free Memory (MB) = %s\n", keyCount, runtime.freeMemory() / mb); }

Slide 11

Slide 11 text

Unique Puts = 1919785 keyCount : Free Memory (MB) = 2 Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded LocalCache myCache = new LocalCache<>(); // 20 MB String value= new String(new char[10000000]); Runtime runtime = Runtime.getRuntime(); int keyCount = 0; int mb = 1024 * 1024; while (true) { myCache.put(keyCount, value); keyCount++; System.out.printf("Unique Puts = %s keyCount : Free Memory (MB) = %s\n", keyCount, runtime.freeMemory() / mb); }

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

MutableConfiguration config = new MutableConfiguration<>(); config.setStoreByValue(true) .setTypes(String.class, Integer.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(EternalExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(TEN_SEC)); Caching.getCachingProvider().getCacheManager().createCache("test", config);

Slide 14

Slide 14 text

MutableConfiguration config = new MutableConfiguration<>(); config.setStoreByValue(true) .setTypes(String.class, Integer.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(EternalExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(TEN_SEC)); Caching.getCachingProvider().getCacheManager().createCache("test", config);

Slide 15

Slide 15 text

MutableConfiguration config = new MutableConfiguration<>(); config.setStoreByValue(true) .setTypes(String.class, Integer.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(ModifiedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(EternalExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(TEN_SEC)); //.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(TEN_SEC)); Caching.getCachingProvider().getCacheManager().createCache("test", config);

Slide 16

Slide 16 text

import javax.cache.Cache; import javax.cache.Caching; Cache myCache = Caching.getCache("test"); // 20 MB String value= new String(new char[10000000]); Runtime runtime = Runtime.getRuntime(); int keyCount = 0; int mb = 1024 * 1024; while (true) { myCache.put(keyCount, value); keyCount++; System.out.printf("Unique Puts = %s keyCount : Free Memory (MB) = %s\n", keyCount, runtime.freeMemory() / mb); }

Slide 17

Slide 17 text

import javax.cache.Cache; import javax.cache.Caching; Cache myCache = Caching.getCache("test"); // 20 MB String value= new String(new char[10000000]); Runtime runtime = Runtime.getRuntime(); int keyCount = 0; int mb = 1024 * 1024; while (true) { myCache.put(keyCount, value); keyCount++; System.out.printf("Unique Puts = %s keyCount : Free Memory (MB) = %s\n", keyCount, runtime.freeMemory() / mb); }

Slide 18

Slide 18 text

import javax.cache.Cache; import javax.cache.Caching; Cache myCache = Caching.getCache("test"); // 20 MB String value= new String(new char[10000000]); Runtime runtime = Runtime.getRuntime(); int keyCount = 0; int mb = 1024 * 1024; while (true) { myCache.put(keyCount, value); keyCount++; System.out.printf("Unique Puts = %s keyCount : Free Memory (MB) = %s\n", keyCount, runtime.freeMemory() / mb); }

Slide 19

Slide 19 text

CACHES

Slide 20

Slide 20 text

public class MyCacheEntryListener implements CacheEntryCreatedListener, CacheEntryUpdatedListener { public void onCreated( Iterable> cacheEntryEvents) throws CacheEntryListenerException { for (CacheEntryEvent entryEvent : cacheEntryEvents) { System.out.println("Created : " + entryEvent.getKey() + " with value : " + entryEvent.getValue()); } } public void onUpdated( Iterable> cacheEntryEvents) throws CacheEntryListenerException { for (CacheEntryEvent entryEvent : cacheEntryEvents) { System.out.println("Updated : " + entryEvent.getKey() + " with value : " + entryEvent.getValue()); } } }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Бизнес-приложение сервис сервис сервис RDBMS Mainframe MongoDB NoSQL REST Масштабирование JCACHE read through cache write through cache

Slide 24

Slide 24 text

/** * Sample of a Read Through CacheLoader */ public class MyCacheLoader implements CacheLoader { public String load(String key) throws CacheLoaderException { if ("Portugal".equals(key)) { return "Lisbon"; } return null; } /** * Async Load Up of a Cache. */ public Map loadAll(Iterable extends String> keys) throws CacheLoaderException { // Iterate the keys and maybe batch load from a DB, // a Filesystem or some other System completely. return null; } }

Slide 25

Slide 25 text

/** * Example of a JCache CacheWriter, use this to write to a RDBMS or somefink. */ public class MyCacheWriter implements CacheWriter { public void write(Cache.Entry extends String, ? extends String> entry) throws CacheWriterException { System.out.println("Write Entry : " + entry.getKey()); } public void writeAll(Collection> entries) throws CacheWriterException { for (Cache.Entry entry : entries) { System.out.println("Write Entry : " + entry.getKey()); } } public void delete(Object key) throws CacheWriterException { } public void deleteAll(Collection> keys) throws CacheWriterException { } }

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

public class AppConfig { public static interface CityService { public String getCity(); } @Bean public CityService getService() { return new CityService() { @Override public String getCity() { // slow code goes here! return result; } }; } }

Slide 28

Slide 28 text

import javax.cache.annotation.CacheResult; import org.springframework.cache.annotation.EnableCaching; @Configuration @EnableCaching public class AppConfig{ public static interface CityService { @CacheResult(cacheName = "city") public String getCity(); } @Bean public CityService getService() { return new CityService() { @Override public String getCity() { // slow code goes here! return result; } }; } }

Slide 29

Slide 29 text

import javax.cache.annotation.CacheResult; import org.springframework.cache.annotation.EnableCaching; @Configuration @EnableCaching public class AppConfig{ public static interface CityService { @CacheResult(cacheName = "city") public String getCity(); } @Bean public CityService getService() { return new CityService() { @Override public String getCity() { // slow code goes here! return result; } }; } }

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

CACHES

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

http://fc07.deviantart.net/fs71/i/2012/156/3/b/bruce_banner_by _lightning_stroke-d52er xe.png

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Caching «загрузчик» CachingProvider «Реализация SPI» CacheManager «управоляет кэшами» Cache «интерфейс к кэшу»

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

CachingProvider cachingProvider = Caching.getCachingProvider(); CacheManager cacheManager = cachingProvider.getCacheManager(); Cache cache = cacheManager.getCache("test");

Slide 43

Slide 43 text

Cache cache = Caching.getCache("test");

Slide 44

Slide 44 text

CachingProvider cachingProvider = Caching.getCachingProvider(); CacheManager cacheManager = cachingProvider.getCacheManager(); Cache cache = cacheManager.getCache("test"); ICache unwrappedCache = cache.unwrap(ICache.class);

Slide 45

Slide 45 text

Cache cache = Caching.getCache("test"); ICache unwrappedCache = cache.unwrap(ICache.class);

Slide 46

Slide 46 text

ICache unwrappedCache = cache.unwrap(ICache.class); ICompletableFuture future = unwrappedCache.getAndPutAsync(1, "value"); future.andThen(new ExecutionCallback() { public void onResponse(String response) { System.out.println("Previous value: " + response); } public void onFailure(Throwable t) { t.printStackTrace(); } });

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Green Primary Green Backup Green Shard

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

public static class CustomCacheMergePolicy implements CacheMergePolicy { @Override public Object merge(String cacheName, CacheEntryView mergingEntry, CacheEntryView existingEntry) { if (mergingEntry.getValue() instanceof Integer) { return mergingEntry.getValue(); } return null; } }

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

ICache iCache1 = cache1.unwrap(ICache.class); iCache1.addPartitionLostListener(new CachePartitionLostListener() { @Override public void partitionLost(CachePartitionLostEvent cachePartitionLostEvent) { System.out.println(cachePartitionLostEvent); latch.countDown(); } });

Slide 56

Slide 56 text

Вместо окончания

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content