Skip to content

Commit

Permalink
#393 移动redis代码到独立模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Sep 13, 2014
1 parent 20d6438 commit 85f705e
Show file tree
Hide file tree
Showing 25 changed files with 662 additions and 546 deletions.
17 changes: 0 additions & 17 deletions modules/extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@
<optional>true</optional>
</dependency>

<!-- NOSQL -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<optional>true</optional>
</dependency>

<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
Expand Down Expand Up @@ -109,11 +97,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.lordofthejars</groupId>
<artifactId>nosqlunit-redis</artifactId>
</dependency>

<!-- Mail -->
<dependency>
<groupId>com.icegreen</groupId>
Expand Down
5 changes: 5 additions & 0 deletions modules/redis/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

echo "[INFO] Install jar to local repository."

mvn clean install
6 changes: 6 additions & 0 deletions modules/redis/install.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off
echo [INFO] Install jar to local repository.

cd %~dp0
call mvn clean install
pause
111 changes: 111 additions & 0 deletions modules/redis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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>
<parent>
<groupId>org.springside</groupId>
<artifactId>springside-parent</artifactId>
<version>4.2.3-SNAPSHOT</version>
<relativePath>../parent/</relativePath>
</parent>
<artifactId>springside-redis</artifactId>
<packaging>jar</packaging>
<name>Springside :: Module :: Redis</name>

<dependencies>
<!-- SPRINGSIDE -->
<dependency>
<groupId>org.springside</groupId>
<artifactId>springside-core</artifactId>
</dependency>

<!-- SPRING, for file loader -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<optional>true</optional>
</dependency>

<!-- TEST -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>

<dependency>
<groupId>com.lordofthejars</groupId>
<artifactId>nosqlunit-redis</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<!-- source attach plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- enforcer, 规则统一定义在parent -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,109 +1,108 @@
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.Pool;

/**
* 装载并执行Lua Script,
*
* 如果服务器上因为集群多台服务器或重启等原因没有装载script,会自动重新装载后重试。
*/
public class JedisScriptExecutor {
private static Logger logger = LoggerFactory.getLogger(JedisScriptExecutor.class);

private JedisTemplate jedisTemplate;

private String script;
private String sha1;

public JedisScriptExecutor(Pool<Jedis> jedisPool) {
this.jedisTemplate = new JedisTemplate(jedisPool);
}

public JedisScriptExecutor(JedisTemplate jedisTemplate) {
this.jedisTemplate = jedisTemplate;
}

/**
* 装载Lua Script。
* 如果Script出错,抛出JedisDataException。
*/
public void load(final String scriptContent) throws JedisDataException {
sha1 = jedisTemplate.execute(new JedisTemplate.JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.scriptLoad(scriptContent);
}
});
script = scriptContent;

logger.debug("Script \"{}\" had been loaded as {}", scriptContent, sha1);
}

/**
* 从文件加载Lua Script, 文件路径格式为Spring Resource的格式.
*/
public void loadFromFile(final String scriptPath) throws JedisDataException {
String scriptContent;
try {
Resource resource = new DefaultResourceLoader().getResource(scriptPath);
scriptContent = FileUtils.readFileToString(resource.getFile());
} catch (IOException e) {
throw new IllegalArgumentException(scriptPath + " is not exist.", e);
}

load(scriptContent);
}

/**
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
* keys与args不允许为null.
*/
public Object execute(final String[] keys, final String[] args) throws IllegalArgumentException {
Validate.notNull(keys, "keys can't be null.");
Validate.notNull(args, "args can't be null.");

return execute(Arrays.asList(keys), Arrays.asList(args));
}

/**
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
* keys与args不允许为null.
*/
public Object execute(final List<String> keys, final List<String> args) throws IllegalArgumentException {
Validate.notNull(keys, "keys can't be null.");
Validate.notNull(args, "args can't be null.");

return jedisTemplate.execute(new JedisAction<Object>() {
@Override
public Object action(Jedis jedis) {
try {
return jedis.evalsha(sha1, keys, args);
} catch (JedisDataException e) {
logger.warn(
"Script {} is not loaded in server yet or the script is wrong, try to reload and run it again.",
script, e);
return jedis.eval(script, keys, args);
}
}
});
}
}
/*******************************************************************************
* Copyright (c) 2005, 2014 springside.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
*******************************************************************************/
package org.springside.modules.nosql.redis;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.util.Pool;

/**
* 装载并执行Lua Script,
*
* 如果服务器上因为集群多台服务器或重启等原因没有装载script,会自动重新装载后重试。
*/
public class JedisScriptExecutor {
private static Logger logger = LoggerFactory.getLogger(JedisScriptExecutor.class);

private JedisTemplate jedisTemplate;

private String script;
private String sha1;

public JedisScriptExecutor(Pool<Jedis> jedisPool) {
this.jedisTemplate = new JedisTemplate(jedisPool);
}

public JedisScriptExecutor(JedisTemplate jedisTemplate) {
this.jedisTemplate = jedisTemplate;
}

/**
* 装载Lua Script。
* 如果Script出错,抛出JedisDataException。
*/
public void load(final String scriptContent) throws JedisDataException {
sha1 = jedisTemplate.execute(new JedisTemplate.JedisAction<String>() {
@Override
public String action(Jedis jedis) {
return jedis.scriptLoad(scriptContent);
}
});
script = scriptContent;

logger.debug("Script \"{}\" had been loaded as {}", scriptContent, sha1);
}

/**
* 从文件加载Lua Script, 文件路径格式为Spring Resource的格式.
*/
public void loadFromFile(final String scriptPath) throws JedisDataException {
String scriptContent;
try {
Resource resource = new DefaultResourceLoader().getResource(scriptPath);
scriptContent = FileUtils.readFileToString(resource.getFile());
} catch (IOException e) {
throw new IllegalArgumentException(scriptPath + " is not exist.", e);
}

load(scriptContent);
}

/**
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
* keys与args不允许为null.
*/
public Object execute(final String[] keys, final String[] args) throws IllegalArgumentException {
Validate.notNull(keys, "keys can't be null.");
Validate.notNull(args, "args can't be null.");
return execute(Arrays.asList(keys), Arrays.asList(args));
}

/**
* 执行Lua Script, 如果Redis服务器上还没装载Script则自动装载并重试。
* keys与args不允许为null.
*/
public Object execute(final List<String> keys, final List<String> args) throws IllegalArgumentException {
Validate.notNull(keys, "keys can't be null.");
Validate.notNull(args, "args can't be null.");

return jedisTemplate.execute(new JedisAction<Object>() {
@Override
public Object action(Jedis jedis) {
try {
return jedis.evalsha(sha1, keys, args);
} catch (JedisDataException e) {
logger.warn(
"Script {} is not loaded in server yet or the script is wrong, try to reload and run it again.",
script, e);
return jedis.eval(script, keys, args);
}
}
});
}
}
Loading

0 comments on commit 85f705e

Please sign in to comment.