hazelcast入门教程_Hazelcast入门指南第5部分
hazelcast入門教程
這是我撰寫的有關Hazelcast的一系列文章的延續。 我強烈建議您閱讀其他內容: 第1 部分 , 第2 部分 , 第3 部分和第4部分 。
一氣呵成的東西
 這篇文章中沒有Hazelcast專用代碼。 讓我重復一遍。 這篇文章中沒有Hazelcast專用代碼。 這是因為Hazelcast的優秀人員生產的產品執行不同的標準。 這樣可以選擇客戶。 Hazelcast實現的那些標準之一是內存緩存。 
JCache呢?
JCache(JSR 107)僅用于Java。 Memcached協議客戶端已跨多種語言實現,因此不能將其固定為一種語言。 在我看來,實施memcached協議是明智之舉,因為它使Hazelcast不僅僅是“ Java事物”。
為什么要使用Hazelcast?
很好的問題! 如果可以使用任何 Memcached服務器,為什么要使用Hazelcast。 好吧,說實話,除非一個人在多臺服務器之間共享一個數據庫,否則甚至可能不需要緩存! 如果確實需要緩存解決方案,這就是為什么我會選擇Hazelcast的原因:
例
我從不喜歡僅顯示“嗡嗡”的示例,因此我將展示Java客戶端如何與Python客戶端共享數據。
建立
我正在使用Java 1.7和Python 3.4。 不幸的是,兩種語言都沒有開箱即用的內存緩存支持,所以我去尋找已經寫好的客戶端。
Java
我發現Spymemcached for Java。 我只是略述其能力的表面。 可以從Maven中獲取。 這是項目的pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.darylmathison</groupId><artifactId>Memcache</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><build><plugin><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><showDeprecation>true</showDeprecation></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.3.2</version><executions><execution><goals><goal>java</goal></goals></execution></executions><configuration><mainClass>com.darylmathison.memcache.Main</mainClass></configuration></plugin></plugins></build><dependencies><dependency><groupId>net.spy</groupId><artifactId>spymemcached</artifactId><version>2.11.5</version></dependency></dependencies></project><dependency><groupId>org.apache.camel</groupId><artifactId>camel-servlet</artifactId></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-metrics</artifactId></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-swagger</artifactId></dependency><dependency><groupId>org.apache.camel</groupId><artifactId>camel-jackson</artifactId></dependency>Python
接下來,我找到了適用于Python的python3-memcached 。 它使用經典的setup.py過程進行安裝。
服務器
如果服務器丟失,則沒有太多的緩存。 人們可以在下載Hazelcast hazelcast.org/download根據自己的操作系統,內容,CD提取到的bin目錄并運行server.bat或服務器腳本。 隨著設置服務器的進行,這是我做過的最簡單的事情。
情況
我試圖使價格更便宜的“昂貴”操作是斐波那契數。 由于Python和Java都可以理解unicode,因此將值存儲為unicode字符串。 密鑰是一個unicode字符串,其中包含序列號或到達該序列所需的輪數。
碼
Java
package com.darylmathison.memcache;import java.io.IOException; import java.net.InetSocketAddress; import net.spy.memcached.MemcachedClient;/**** @author Daryl*/ public class Main {/*** @param args the command line arguments*/public static void main(String[] args) {try {MemcachedClient client = new MemcachedClient(new InetSocketAddress("localhost", 5701));for(int i = 2; i < 20; i++) {System.out.println("value of round " + i + " is " + fibonacci(i, client));}client.shutdown();} catch(IOException ioe) {ioe.printStackTrace();}}private static long fibonacci(int rounds, MemcachedClient client) {String cached = (String)client.get(String.valueOf(rounds));if(cached != null) {System.out.print("cached ");return Long.parseLong(cached);}long[] lastTwo = new long[] {1, 1};for(int i = 0; i < rounds; i++) {long last = lastTwo[1];lastTwo[1] = lastTwo[0] + lastTwo[1];lastTwo[0] = last;}client.set(String.valueOf(rounds), 360, String.valueOf(lastTwo[1]));return lastTwo[1];} }Python
這是Python客戶端。 作為一名pythonian,我嘗試成為盡可能的pythonic。
import memcacheclient = memcache.Client(['localhost:5701'])def fibonacci(round):f = [1, 1, 1]for i in range(round):f[-1] = sum(f[:2])f[0], f[1] = f[1], f[2]return f[2]def retrievefib(round):fib = client.get(str(round))if not fib:fib = fibonacci(round)client.set(str(round), str(fib))else:print("cached")return fibdef main():store = [ x for x in range(20) if x % 2 == 0]for i in store:retrievefib(i)for i in range(20):print(retrievefib(i))if __name__ == "__main__":main()結論
好吧,這是Hazelcast作為幕后力量的例子。 我認為這是最耀眼的地方。 無需創建全新的,巧妙的分布式應用程序即可利用Hazelcast。 所有要做的就是使用已知的做法,并讓Hazelcast進行艱苦的工作。 這個崗位的來源,可以發現這里的Java代碼,并在這里為Python代碼。
參考資料
- http://en.wikipedia.org/wiki/Fibonacci_number
 - https://code.google.com/p/spymemcached/
 - https://pypi.python.org/pypi/python3-memcached/1.51
 
翻譯自: https://www.javacodegeeks.com/2014/12/the-beginners-guide-to-hazelcast-part-5.html
hazelcast入門教程
總結
以上是生活随笔為你收集整理的hazelcast入门教程_Hazelcast入门指南第5部分的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: linux解压gz文件的命令(linux
 - 下一篇: spring依赖注入_Spring的依赖