package com.cmcc.couchbase;
import java.net.URI;
import java.util.ArrayList;
import com.couchbase.client.CouchbaseConnectionFactory;
public class CouchbaseConnector {
private String serverAddress;
private String name;
private String pwd;
public CouchbaseConnectionFactory connect() {
CouchbaseConnectionFactory cf = null;
try {
String[] serverNames = serverAddress.split(",");
ArrayList<URI> serverList = new ArrayList<URI>();
for (String serverName : serverNames) {
URI base = null;
base = URI.create(String.format("http://%s/pools",serverName));
serverList.add(base);
}
cf = new CouchbaseConnectionFactory(serverList, name, pwd);
return cf;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void close(){
}
public String getServerAddress() {
return serverAddress;
}
public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package com.cmcc.couchbase;
import java.io.IOException;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.View;
public class CouchbaseTemplate extends CouchbaseClient implements ICouchbaseTemplate
{
public CouchbaseTemplate(CouchbaseConnector couchbaseConnector) throws IOException
{
super(couchbaseConnector.connect());
}
public void addRecord(String key, Object value)
{
this.add(key, 0, value);
}
public Object getRecord(String key)
{
return this.get(key);
}
}
package com.cmcc.couchbase;
import com.couchbase.client.CouchbaseClientIF;
public interface ICouchbaseTemplate extends CouchbaseClientIF
{
public void addRecord(String key,Object value);
public Object getRecord(String key);
public Object get(String key);
}
applicationContext.xml
<bean id="couchbaseConnector" class="com.cmcc.couchbase.CouchbaseConnector"
destroy-method="close">
<property name="serverAddress">
<value>${couchBase.server}</value>
</property>
<property name="name">
<value>${couchBase.name}</value>
</property>
<property name="pwd">
<value>${couchBase.pwd}</value>
</property>
</bean>
<bean id="couchbaseTemplate" class="com.cmcc.couchbase.CouchbaseTemplateMock">
<constructor-arg>
<ref bean="couchbaseConnector" />
</constructor-arg>
</bean>
pom.xml
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>couchbase-client</artifactId>
<version>1.2.1</version>
<type>jar</type>
</dependency>
使用
@Service
@Transactional
public class AppInfoService {
@Resource
ICouchbaseTemplate couchbaseTemplate;
public AppInfo getAppInfo(String appid) {
Gson gson = new Gson();
String json = (String) couchbaseTemplate.get("AppInfo-AppId:" + appid);
AppInfo appInfo = null;
if(json != null)
appInfo = gson.fromJson(json, AppInfo.class);
return appInfo;
}
}
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!