JavaEE學習日誌(一百一十八): 品優購項目筆記(三)

angularJS頁面之間傳參

一、html頁面跳轉到另一個html頁面傳參數
angularjs規定頁面跳轉到頁面並且傳參數語法爲: 頁面#?參數名=參數值
二、html頁面中接收另一個頁面傳過來的參數:
angularjs規定語法: $location.search()['參數名'];
$location是angularjs的內置對象, 裏面有search方法, 用來搜索其他頁面傳入這裏的參數

redis分佈式緩存

redis底層使用C語言編寫, 存儲數據是放在內存中, 速度非常快.
作用:
redis在互聯網項目中一般充當分佈式緩存使用

業務流程:
獲取數據的時候先從redis中獲取, 如果獲取到數據則直接返回, 就不用訪問數據庫了
如果獲取不到數據, 可以從數據庫中查詢, 查詢到後放入redis中一份, 下回就可以直接從redis中查詢到,這樣大大降低了數據庫的高併發訪問壓力.

持久化方案:

  1. rdb(默認) 分時持久化
    可以在配置文件中設定, 多長時間持久化一次, 持久化次數少也就是操作硬盤的次數少,速度快. 但是如果在沒有完成持久化前, 如果服務器斷電, 則內存中沒有持久化的數據會丟失.

  2. aop 實時持久化
    每次向redis中做增刪改操作, 都會將數據持久化到硬盤上, 數據可靠性高, 不會丟失,
    但是速度慢

redis中數據類型: string, set, zset, list, hash

redis同類型技術:
memcache是redis的同類型技術, 底層也是使用c語言編寫, 現在memcache已經被redis替代了.memcache的速度和redis相當. 但是memcache沒有持久化方式.

mongodb和redis區別:
mongodb也是一個nosql數據庫, 存儲的數據是非結構化的, 但是mongdb和redis不能放在一起對比,因爲完全沒有可比性, 使用場景完全不同

  • redis: 主要使用內存, 有兩種持久化方案, 速度非常快, 一般做分佈式緩存使用
  • mongodb: 主要使用硬盤存儲, 所以不會擔心數據丟失, 速度介於redis和傳統數據庫之間.但是mongodb更擅長存儲大文本數據, 以及一些非結構化數據, mongodb比redis的數據類型更加豐富.
    例如: 存儲小說網站的小說, 存儲電商網站的評論等這些數據

SpringDataRedis

Spring-data-redis是spring大家族的一部分,提供了在srping應用中通過簡單的配置訪問redis服務,對reids底層開發包(Jedis, JRedis, and RJC)進行了高度封裝,RedisTemplate提供了redis各種操作、異常處理及序列化,支持發佈訂閱,並對spring 3.1 cache進行了實現。
spring-data-redis針對jedis提供瞭如下功能:

  1. 連接池自動管理,提供了一個高度封裝的“RedisTemplate”類
  2. 針對jedis客戶端中大量api進行了歸類封裝,將同一類型操作封裝爲operation接口
    ValueOperations:簡單K-V操作
    SetOperations:set類型數據操作
    ZSetOperations:zset類型數據操作
    HashOperations:針對map類型的數據操作
    ListOperations:針對list類型的數據操作

操作string類型的數據

一、引入依賴

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- 緩存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>

    </dependencies>

二、引入屬性文件

redis.host=192.168.200.128
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

三、引入xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath*:*.properties" />
    <!-- redis 相關配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory" />
    </bean>
</beans>

四、代碼實現

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestString {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testSet(){
        redisTemplate.boundValueOps("testKey").set("xxxxx");

    }

    @Test
    public void testGet(){
        String testKey = (String) redisTemplate.boundValueOps("testKey").get();
        System.out.println(testKey);
    }

    @Test
    public void testDelete(){
        redisTemplate.delete("testKey");
    }

}

操作hash類型的數據

redis就相當於一個大的hashmap,他的值是hash,那麼value也相當於一個hashmap
相當於HashMap<String,HashMap<String,value>

Hash類型是無序的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestHash {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testPut(){
        redisTemplate.boundHashOps("testHash").put("001","青龍");
        redisTemplate.boundHashOps("testHash").put("002","白虎");
        redisTemplate.boundHashOps("testHash").put("003","朱雀");
        redisTemplate.boundHashOps("testHash").put("004","玄武");
    }

    @Test
    public void testGetOne(){
        String value = (String) redisTemplate.boundHashOps("testHash").get("003");
        System.out.println(value);
    }

    @Test
    public void testGetAll(){
        Map<String,String> map = (Map<String,String>) redisTemplate.boundHashOps("testHash").entries();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"+"+value);
        }
    }

    @Test
    public void testDeleteOne(){
        redisTemplate.boundHashOps("testHash").delete("003");
    }

    @Test
    public void testDeleteAll(){
        redisTemplate.delete("testHash");
    }

}

操作list類型的數據

list有序,裏面存入的數據可以重複

添加數據方式:左插入或右插入
左插入:相當於棧
右插入:相當於隊列

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class TestList {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testLpush(){
        redisTemplate.boundListOps("testList").leftPush("1");
        redisTemplate.boundListOps("testList").leftPush("2");
        redisTemplate.boundListOps("testList").leftPush("3");
        redisTemplate.boundListOps("testList").leftPush("4");
    }

    @Test
    public void testRpush(){
        redisTemplate.boundListOps("testList").rightPush("1");
        redisTemplate.boundListOps("testList").rightPush("2");
        redisTemplate.boundListOps("testList").rightPush("3");
        redisTemplate.boundListOps("testList").rightPush("4");
    }

    @Test
    public void testRange(){
        List<String> testList = redisTemplate.boundListOps("testList").range(0, 10);
        for (String s : testList) {
            System.out.println(s);
        }
    }

    @Test
    public void testDelete(){
        redisTemplate.delete("testList");
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章