Skip to content

Commit

Permalink
去除redis-shiro jar依赖,代码实现
Browse files Browse the repository at this point in the history
  • Loading branch information
lcg0124 committed Jan 8, 2018
1 parent 7d9d12e commit 311c00d
Show file tree
Hide file tree
Showing 7 changed files with 717 additions and 6 deletions.
10 changes: 5 additions & 5 deletions bootdo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@
<version>1.9.2</version>
</dependency>

<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>2.4.2.1-RELEASE</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.crazycake</groupId>-->
<!--<artifactId>shiro-redis</artifactId>-->
<!--<version>2.4.2.1-RELEASE</version>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.bootdo.activiti.service.ProcessService;
import com.bootdo.activiti.vo.ProcessVO;
import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.R;
import org.activiti.engine.ActivitiException;
Expand All @@ -27,7 +29,7 @@

@RequestMapping("activiti/process")
@RestController
public class ProcessController {
public class ProcessController extends BaseController{

@Autowired
private RepositoryService repositoryService;
Expand Down Expand Up @@ -114,6 +116,9 @@ public R deploy(String exportDir, String category, MultipartFile file) {
*/
@RequestMapping(value = "/convertToModel/{procDefId}")
public R convertToModel(@PathVariable("procDefId") String procDefId, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException, XMLStreamException {
if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
return R.error(1, "演示系统不允许修改,完整体验请部署程序");
}
org.activiti.engine.repository.Model modelData = null;
try {
modelData = processService.convertToModel(procDefId);
Expand All @@ -137,6 +142,9 @@ public void resourceRead(@PathVariable("xml") String resType, @PathVariable("id"

@PostMapping("/remove")
public R remove(String id){
if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
return R.error(1, "演示系统不允许修改,完整体验请部署程序");
}
repositoryService.deleteDeployment(id,true);
return R.ok();
}
Expand Down
195 changes: 195 additions & 0 deletions bootdo/src/main/java/com/bootdo/common/redis/shiro/RedisCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.bootdo.common.redis.shiro;

/**
* @author bootdo [email protected]
* @version V1.0
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.SerializeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RedisCache<K, V> implements Cache<K, V> {

private Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* The wrapped Jedis instance.
*/
private RedisManager cache;

/**
* The Redis key prefix for the sessions
*/
private String keyPrefix = "shiro_redis_session:";

/**
* Returns the Redis session keys
* prefix.
* @return The prefix
*/
public String getKeyPrefix() {
return keyPrefix;
}

/**
* Sets the Redis sessions key
* prefix.
* @param keyPrefix The prefix
*/
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}

/**
* 通过一个JedisManager实例构造RedisCache
*/
public RedisCache(RedisManager cache){
if (cache == null) {
throw new IllegalArgumentException("Cache argument cannot be null.");
}
this.cache = cache;
}

/**
* Constructs a cache instance with the specified
* Redis manager and using a custom key prefix.
* @param cache The cache manager instance
* @param prefix The Redis key prefix
*/
public RedisCache(RedisManager cache,
String prefix){

this( cache );

// set the prefix
this.keyPrefix = prefix;
}

/**
* 获得byte[]型的key
* @param key
* @return
*/
private byte[] getByteKey(K key){
if(key instanceof String){
String preKey = this.keyPrefix + key;
return preKey.getBytes();
}else{
return SerializeUtils.serialize(key);
}
}

@Override
public V get(K key) throws CacheException {
logger.debug("根据key从Redis中获取对象 key [" + key + "]");
try {
if (key == null) {
return null;
}else{
byte[] rawValue = cache.get(getByteKey(key));
@SuppressWarnings("unchecked")
V value = (V)SerializeUtils.deserialize(rawValue);
return value;
}
} catch (Throwable t) {
throw new CacheException(t);
}

}

@Override
public V put(K key, V value) throws CacheException {
logger.debug("根据key从存储 key [" + key + "]");
try {
cache.set(getByteKey(key), SerializeUtils.serialize(value));
return value;
} catch (Throwable t) {
throw new CacheException(t);
}
}

@Override
public V remove(K key) throws CacheException {
logger.debug("从redis中删除 key [" + key + "]");
try {
V previous = get(key);
cache.del(getByteKey(key));
return previous;
} catch (Throwable t) {
throw new CacheException(t);
}
}

@Override
public void clear() throws CacheException {
logger.debug("从redis中删除所有元素");
try {
cache.flushDB();
} catch (Throwable t) {
throw new CacheException(t);
}
}

@Override
public int size() {
try {
Long longSize = new Long(cache.dbSize());
return longSize.intValue();
} catch (Throwable t) {
throw new CacheException(t);
}
}

@SuppressWarnings("unchecked")
@Override
public Set<K> keys() {
try {
Set<byte[]> keys = cache.keys(this.keyPrefix + "*");
if (CollectionUtils.isEmpty(keys)) {
return Collections.emptySet();
}else{
Set<K> newKeys = new HashSet<K>();
for(byte[] key:keys){
newKeys.add((K)key);
}
return newKeys;
}
} catch (Throwable t) {
throw new CacheException(t);
}
}

@Override
public Collection<V> values() {
try {
Set<byte[]> keys = cache.keys(this.keyPrefix + "*");
if (!CollectionUtils.isEmpty(keys)) {
List<V> values = new ArrayList<V>(keys.size());
for (byte[] key : keys) {
@SuppressWarnings("unchecked")
V value = get((K)key);
if (value != null) {
values.add(value);
}
}
return Collections.unmodifiableList(values);
} else {
return Collections.emptyList();
}
} catch (Throwable t) {
throw new CacheException(t);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.bootdo.common.redis.shiro;

/**
* @author bootdo [email protected]
* @version V1.0
*/
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.crazycake.shiro.RedisManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RedisCacheManager implements CacheManager {

private static final Logger logger = LoggerFactory
.getLogger(RedisCacheManager.class);

// fast lookup by name map
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();

private RedisManager redisManager;

/**
* The Redis key prefix for caches
*/
private String keyPrefix = "shiro_redis_cache:";

/**
* Returns the Redis session keys
* prefix.
* @return The prefix
*/
public String getKeyPrefix() {
return keyPrefix;
}

/**
* Sets the Redis sessions key
* prefix.
* @param keyPrefix The prefix
*/
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}

@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
logger.debug("获取名称为: " + name + " 的RedisCache实例");

Cache c = caches.get(name);

if (c == null) {

// initialize the Redis manager instance
redisManager.init();

// create a new cache instance
c = new RedisCache<K, V>(redisManager, keyPrefix);

// add it to the cache collection
caches.put(name, c);
}
return c;
}

public RedisManager getRedisManager() {
return redisManager;
}

public void setRedisManager(RedisManager redisManager) {
this.redisManager = redisManager;
}

}
Loading

0 comments on commit 311c00d

Please sign in to comment.