引入spring-data-redis包、jedis、connection-pool包
applicationContext.xml的配置
<bean id="redisConnection" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost">property> <property name="port" value="6379">property> bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnection">property> bean>
做一个简单的测试
@Test public void test1() { Jedis jd = new Jedis("localhost",6379); String ping = jd.ping(); System.out.println(ping); Setkeys = jd.keys("*"); for(String k:keys){ System.out.println(k + ":"+ jd.type(k)); } jd.close(); } @Test public void test2(){ Jedis j=new Jedis("localhost", 6379); System.out.println(j.ping()); Set filed= j.hkeys("dept"); System.out.println(filed); for(String s:filed){ System.out.println(j.hget("dept", s)); } j.close(); }
自己写一个工具类将,进行序列化与反序列化,
public class SerializableUtil { public static byte[] objectToBytes(Object obj) {// 将对象转换为byte数组 ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(obj);// 将数据序列化后写入到baos中 byte[] byte1 = baos.toByteArray(); return byte1; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } finally { try { baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static Object byteToObject(byte[] bytes) { ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); Object obj = ois.readObject(); return obj; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } finally { try { bais.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
测试:
@Test public void test4(){ DeptBean bean=new DeptBean(124, "zhangsan1", "shanghai"); Jedis j=new Jedis("localhost", 6379); j.set("dept".getBytes(), SerializableUtil.objectToBytes(bean)); j.close(); } @Test public void test5(){ Jedis j=new Jedis("localhost", 6379); byte[] bean=j.get("dept".getBytes());//获取到的是byte数组 //在将byte数组反序列化 DeptBean byteToObject = (DeptBean) SerializableUtil.byteToObject(bean); System.out.println(byteToObject); j.close(); }
上面只是简单的使用自己写的一个工具类进行序列化与反序列化,实际开发中还是使用工具进行的,
写一个控制器,对其进行单元测试
@RunWith(SpringJUnit4ClassRunner.class)//这里的意思是进行一个spring环境的配置 @ContextConfiguration(locations = "classpath:applicationContext.xml")//让其能不启动tomcat服务器的情况下进行测试 public class TestRedis_Data { @Autowired//自动装载 public RedisTemplate
然后在对自己有缓存需求的方法进行开启Redis缓存,
//@Controller @RestController //相当于@Controller 和 @ResponseBody 相结合的功能 public class DeptController { @Autowired //自动装载 private DeptDao dao; //使用redis @Autowired private RedisTemplate
当然如果你需要在项目启动时候就加载到内存中,则可以这样,另写一个专门加载需要缓存的数据
@Component public class InitLoadData { @Resource //装载dao层 private DeptDao dao; @Resource private RedisTemplate