`

未完成的redis集成

阅读更多


springmvc + mybatis +redis  没有调研完成的 缓存引入,
查找了各种资料,包括【Iteye上的大神:http://liuyieyer.iteye.com/blog/2081382
【github上的大神:https://github.com/mybatis/redis-cache

package xx.redis;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.ibatis.cache.Cache;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

public final class RedisCache implements Cache {

 private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

 private String id;

 public RedisCache(final String id) {
  if (id == null) {
   throw new IllegalArgumentException("Cache instances require an ID");
  }
  this.id = id;
 }

 private Object execute(RedisCallback callback) {
  Jedis jedis = CachePool.getInstance().getJedis();
  try {
   return callback.doWithRedis(jedis);
  } finally {
   // jedis.close();
   jedis.quit();
  }
 }

 public String getId() {
  return this.id;
 }

 public int getSize() {
  return (Integer) execute(new RedisCallback() {
   public Object doWithRedis(Jedis jedis) {
    Map<byte[], byte[]> result = jedis.hgetAll(id.toString().getBytes());
    return result.size();
   }
  });
 }

 public void putObject(final Object key, final Object value) {
  execute(new RedisCallback() {
   public Object doWithRedis(Jedis jedis) {
    jedis.hset(id.toString().getBytes(), key.toString().getBytes(), SerializeUtil.serialize(value));
    return null;
   }
  });
 }

 public Object getObject(final Object key) {
  return execute(new RedisCallback() {
   public Object doWithRedis(Jedis jedis) {
    Object val = SerializeUtil.unserialize(jedis.hget(id.toString().getBytes(), key.toString().getBytes()));
    System.out.println("get from redis:id=" + key + ",val=" + val);
    return val;
   }
  });
 }

 public Object removeObject(final Object key) {
  return execute(new RedisCallback() {
   public Object doWithRedis(Jedis jedis) {
    return jedis.hdel(id.toString(), key.toString());
   }
  });
 }

 public void clear() {
  execute(new RedisCallback() {
   public Object doWithRedis(Jedis jedis) {
    jedis.del(id.toString());
    return null;
   }
  });

 }

 public ReadWriteLock getReadWriteLock() {
  return readWriteLock;
 }

 @Override
 public String toString() {
  return "Redis {" + id + "}";
 }

 /**
  *
  * @ClassName: CachePool
  * @Description: TODO(单例Cache池)
  * @author LiuYi
  */
 public static class CachePool {
  JedisPool pool;
  private static final CachePool cachePool = new CachePool();

  public static CachePool getInstance() {
   return cachePool;
  }

  private CachePool() {
   JedisPoolConfig config = new JedisPoolConfig();
   Properties p = new Properties();
   InputStream f = CachePool.class.getClassLoader().getResourceAsStream("redis.properties");
   try {
    p.load(f);
   } catch (IOException e) {
    e.printStackTrace();
   }
   config.setMaxIdle(Integer.valueOf(p.getProperty("redis.maxIdle")));
   config.setMaxWait(Integer.valueOf(p.getProperty("redis.maxWait")));
   pool = new JedisPool(config, p.getProperty("redis.host"));
  }

  public Jedis getJedis() {
   Jedis jedis = null;
   boolean borrowOrOprSuccess = true;
   try {
    jedis = pool.getResource();
   } catch (JedisConnectionException e) {
    borrowOrOprSuccess = false;
    if (jedis != null)
     pool.returnBrokenResource(jedis);
   } finally {
    if (borrowOrOprSuccess)
     pool.returnResource(jedis);
   }
   jedis = pool.getResource();
   return jedis;
  }

  public JedisPool getJedisPool() {
   return this.pool;
  }

 }
}

================================================

package xx.redis;
import redis.clients.jedis.Jedis;

public interface RedisCallback {

 Object doWithRedis(Jedis jedis);
}

================================================

package xx.redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.ibatis.cache.CacheException;

public final class SerializeUtil {

 public static byte[] serialize(Object object) {
  ObjectOutputStream oos = null;
  ByteArrayOutputStream baos = null;
  try {
   baos = new ByteArrayOutputStream();
   oos = new ObjectOutputStream(baos);
   oos.writeObject(object);
   byte[] bytes = baos.toByteArray();
   return bytes;
  } catch (Exception e) {
   throw new CacheException(e);
  }
 }

 public static Object unserialize(byte[] bytes) {
  if (bytes == null) {
   return null;
  }
  ByteArrayInputStream bais = null;
  try {
   bais = new ByteArrayInputStream(bytes);
   ObjectInputStream ois = new ObjectInputStream(bais);
   return ois.readObject();
  } catch (Exception e) {
   throw new CacheException(e);
  }
 }

}

================================================
redis.properties 文件:

#redis configuration
redis.host=172.16.180.75
redis.port=6379
redis.pass=
redis.default.db=0
redis.timeout=100000
redis.maxActive=300
redis.maxIdle=100
redis.maxWait=1000
DBSync.testOnBorrow=true

====================================================
mybatis-config.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <settings>
  <setting name="cacheEnabled" value="true" />
<!--   <setting name="lazyLoadingEnabled" value="true" /> -->
<!--   <setting name="multipleResultSetsEnabled" value="true" /> -->
<!--   <setting name="useColumnLabel" value="true" /> -->
<!--   <setting name="useGeneratedKeys" value="false" /> -->
<!--   <setting name="enhancementEnabled" value="false" /> -->
<!--   <setting name="defaultExecutorType" value="SIMPLE" /> -->
 </settings>
</configuration>

 

spring-mybatis文件
<!-- mybatis文件配置,扫描所有mapper文件 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="mapperLocations">
   <array>
    <value>classpath:conf-mybatis/ext/*Mapper.xml</value>
    <value>classpath:conf-mybatis/*Mapper.xml</value>
   </array>
  </property>
  <property name="configLocation" value="classpath:conf-mybatis/mybatis-config.xml" />
  <property name="dataSource" ref="dataSource" />
  <property name="plugins">
   <ref bean="pageHelper" />
  </property>
 </bean>
=============================================================

 

 

 

 


 

分享到:
评论

相关推荐

    ubuntu 14.04 ambari平台集成redis集群

    - Redis官方并未直接提供Ambari集成,但可以通过自定义服务脚本来实现。你可以使用附件中的部署文档来创建自定义服务 - 编写并配置Ambari Blueprint,包含Redis集群的配置和服务依赖 - 将自定义服务脚本上传至...

    redis源码安装以及配置

    **三、PHP与Redis集成** PHP通过`phpredis`扩展与Redis进行交互。`phpredis-2.2.4.tar.gz`正是这个扩展的源码包。 1. **解压与安装** 解压`phpredis`源码,然后按照PHP扩展的常规流程进行安装: ``` tar -zxvf ...

    idea连接redis可视化工具插件

    - 自动完成Redis命令,提高输入效率。 - 错误提示和调试支持,帮助开发者定位问题。 总之,Iedis作为Idea的Redis可视化工具插件,提供了一站式的Redis操作体验,简化了开发过程中的数据管理任务,提升了开发效率...

    redis整合到tomcat集群的支持

    另外,要确保Redis服务器的安全,避免未授权访问,可以通过设置访问控制、使用SSL等方式提升安全性。 总结,将Redis整合到Tomcat集群中,主要涉及Redis的使用、Tomcat的会话管理机制、配置自定义Session Manager...

    magento -redis 插件安装包

    2. **Magento与Redis集成的意义**: - 性能优化:通过Redis的内存存储,提高数据读取速度,改善用户购物体验。 - 数据一致性:Redis支持事务处理,确保在并发环境下数据的一致性。 - 扩展性:Redis的分布式特性...

    redis主从配置及通过keepalived实现redis自动切换

    3. Redis 主从配置与 Keepalived 的集成: 通过使用 Keepalived,可以实现 Redis 的自动切换。当 Master 挂掉时,Keepalived 可以自动将 Slave提升为 Master,并关闭主从复制功能;当 Master 恢复正常时,...

    Redis 安装文件 + c#版本(unity)

    安装完成后,Redis服务器将在后台运行,你可以通过命令行工具与之交互,例如使用`redis-cli`来执行各种命令。 接着是 `RedisClient.rar` 文件,这可能是一个C#实现的Redis客户端库。在Unity中,与Redis通信通常需要...

    定向爬虫:Scrapy与Redis入门

    Item Pipeline可以与Redis集成,将抓取的数据实时存入Redis,然后由后端应用或者其他的Scrapy实例进行进一步处理。例如,可以将数据暂存到Redis的列表中,等待批量导入到数据库,或者通过发布订阅机制实时推送数据到...

    redis_exporter-v0.13.linux-amd64.tar.gz

    安装和配置完成后,Prometheus 将定期抓取 Redis Exporter 提供的指标,并可以与 Grafana 等可视化工具集成,展示直观的监控面板。这样,你可以实时观察 Redis 的性能和健康状况,及时发现并解决问题。 总结来说,...

    Windows 系统 Redis-x64-3.2.100安装包 - 已压缩

    - **持续集成/部署**:在 Windows 上使用 Redis 可能需要调整 CI/CD 流程,因为许多自动化工具和脚本可能假设 Redis 运行在 Linux 环境中。 总之,Redis-x64-3.2.100 是 Windows 用户一个可靠的选择,尤其对于那些...

    windows 部署redis 服务端和客户端实战资源

    - 配置文件`redis.conf`(虽未提供,但通常是必备的),用于设置Redis实例的行为,如端口、数据持久化、内存限制等。 - 安装完成后,需通过命令行启动服务或使用管理工具,如`mgr-redis部署使用.docx`中可能包含的...

    idea下springboot整合redis例子

    至此,你已经完成了Spring Boot整合Redis的基础配置和使用。这个例子简单易懂,适合初学者作为入门示例。你可以通过运行`CacheController`中的接口进行测试,验证Redis的存取功能是否正常工作。 这个压缩包中的...

    redis倒计时商品订单状态

    9. **测试与调试**:开发完成后,要进行充分的单元测试和集成测试,确保功能的正确性和性能。可以利用Mockito等工具模拟Redis服务,便于单元测试。 10. **性能优化**:考虑到大量订单的处理,可以采用消息队列(如...

    redis锁redis锁

    即使客户端崩溃或未正确释放锁,Redis也会在预设时间后自动删除该锁。 3. **UNLOCK操作**:客户端在完成操作后需要释放锁,这可以通过`DEL`命令删除键或使用`SCRIPT`执行原子操作来实现。 **Jedis实现Redis锁** 1. ...

    window下redis安装包完美兼容版

    Redis的配置主要通过修改`redis.windows.conf`文件来完成。在文件中,你可以设置端口号(默认6379)、数据库数量、是否允许远程访问、日志文件路径等参数。例如,开启远程连接需将`bind 127.0.0.1`行注释掉,并取消...

    Redis-x64-3.2.100.zip

    集成Redis,可以利用其作为分布式缓存,提高应用性能;也可以作为消息队列,实现异步处理和解耦。 7. **Redis在Windows的运行与监控**:由于Redis默认以守护进程模式运行,Windows用户可能需要使用第三方工具(如`...

    适配与Tomcat7、8、9的redis session共享jar包

    **Tomcat与Redis的集成** Tomcat的session共享机制是通过实现`javax.servlet.http.HttpSessionBindingListener`和`javax.servlet.http.HttpSessionAttributeListener`接口的session管理器来完成的。在本案例中,`...

    redis-window安装使用说明.doc

    安装完成后,需要对Redis服务进行一些配置。使用文本编辑器,比如Notepad++,打开redis.windows-service.conf配置文件。注意,这个是用于以服务方式运行Redis的配置文件,而不是redis.windows.conf。在配置文件中,...

    redis windows 版本安装(亲测可用)

    - 下载完成后,解压缩文件到你希望安装Redis的目录,例如 `C:\Program Files\Redis`。 - 打开解压后的文件夹,你会看到多个.exe可执行文件,如`redis-server.exe`、`redis-cli.exe`等,这些都是Redis的主要组件。 ...

    Thinkphp6 redis队列 消息事件 gatewayworker聊天打通版

    4. `GatewayWorker`接收到消息后,广播到对应的用户连接,完成消息传递。 在`tp6gatewayworker`这个项目中,可能包含了`Thinkphp6`的源码、配置文件、`Redis`的队列配置、`GatewayWorker`的服务配置和启动脚本等。...

Global site tag (gtag.js) - Google Analytics