当前位置: 首页>>代码示例>>Java>>正文


Java Cache类代码示例

本文整理汇总了Java中com.android.volley.Cache的典型用法代码示例。如果您正苦于以下问题:Java Cache类的具体用法?Java Cache怎么用?Java Cache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Cache类属于com.android.volley包,在下文中一共展示了Cache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: publicMethods

import com.android.volley.Cache; //导入依赖的package包/类
@Test
public void publicMethods() throws Exception {
    // Catch-all test to find API-breaking changes.
    assertNotNull(Response.class.getMethod("success", Object.class, Cache.Entry.class));
    assertNotNull(Response.class.getMethod("error", VolleyError.class));
    assertNotNull(Response.class.getMethod("isSuccess"));

    assertNotNull(Response.Listener.class.getDeclaredMethod("onResponse", Object.class));

    assertNotNull(Response.ErrorListener.class.getDeclaredMethod("onErrorResponse",
            VolleyError.class));

    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class, long.class));
    assertNotNull(NetworkResponse.class.getConstructor(int.class, byte[].class, Map.class,
            boolean.class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class));
    assertNotNull(NetworkResponse.class.getConstructor(byte[].class, Map.class));
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:20,代码来源:ResponseTest.java

示例2: parseCacheHeaders_cacheControlMustRevalidateWithMaxAgeAndStale

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_cacheControlMustRevalidateWithMaxAgeAndStale() {
    long now = System.currentTimeMillis();
    headers.put("Date", rfc1123Date(now));
    headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));

    // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day
    // - stale-while-revalidate (entry.ttl) indicates that the asset may
    // continue to be served stale for up to additional 7 days, but this is
    // ignored in this case because of the must-revalidate header.
    headers.put("Cache-Control",
            "must-revalidate, max-age=86400, stale-while-revalidate=604800");

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
    assertNotNull(entry);
    assertNull(entry.etag);
    assertEqualsWithin(now + ONE_DAY_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS);
    assertEquals(entry.softTtl, entry.ttl);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:19,代码来源:HttpHeaderParserTest.java

示例3: parseCaseInsensitive

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCaseInsensitive() {

        long now = System.currentTimeMillis();

        Header[] headersArray = new Header[5];
        headersArray[0] = new BasicHeader("eTAG", "Yow!");
        headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
        headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
        headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
        headersArray[4] = new BasicHeader("content-type", "text/plain");

        Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
        NetworkResponse response = new NetworkResponse(0, null, headers, false);
        Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

        assertNotNull(entry);
        assertEquals("Yow!", entry.etag);
        assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
        assertEquals(entry.softTtl, entry.ttl);
        assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
    }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:22,代码来源:HttpHeaderParserTest.java

示例4: cacheHeaderSerialization

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void cacheHeaderSerialization() throws Exception {
    Cache.Entry e = new Cache.Entry();
    e.data = new byte[8];
    e.serverDate = 1234567L;
    e.lastModified = 13572468L;
    e.ttl = 9876543L;
    e.softTtl = 8765432L;
    e.etag = "etag";
    e.responseHeaders = new HashMap<String, String>();
    e.responseHeaders.put("fruit", "banana");

    CacheHeader first = new CacheHeader("my-magical-key", e);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    first.writeHeader(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CacheHeader second = CacheHeader.readHeader(bais);

    assertEquals(first.key, second.key);
    assertEquals(first.serverDate, second.serverDate);
    assertEquals(first.lastModified, second.lastModified);
    assertEquals(first.ttl, second.ttl);
    assertEquals(first.softTtl, second.softTtl);
    assertEquals(first.etag, second.etag);
    assertEquals(first.responseHeaders, second.responseHeaders);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:DiskBasedCacheTest.java

示例5: makeRandomCacheEntry

import com.android.volley.Cache; //导入依赖的package包/类
/**
 * Makes a random cache entry.
 * @param data Data to use, or null to use random data
 * @param isExpired Whether the TTLs should be set such that this entry is expired
 * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
 */
public static Cache.Entry makeRandomCacheEntry(
        byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.lastModified = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:22,代码来源:CacheTestUtils.java

示例6: testParseCacheHeaders_staleWhileRevalidate

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void testParseCacheHeaders_staleWhileRevalidate() {
    long now = System.currentTimeMillis();
    headers.put("Date", rfc1123Date(now));
    headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));

    // - max-age (entry.softTtl) indicates that the asset is fresh for 1 day
    // - stale-while-revalidate (entry.ttl) indicates that the asset may
    // continue to be served stale for up to additional 7 days
    headers.put("Cache-Control", "max-age=86400, stale-while-revalidate=604800");

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertNull(entry.etag);
    assertEqualsWithin(now + ONE_DAY_MILLIS, entry.softTtl, ONE_MINUTE_MILLIS);
    assertEqualsWithin(now + ONE_DAY_MILLIS + ONE_WEEK_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:18,代码来源:HttpHeaderParserTest.java

示例7: requestOkHttp

import com.android.volley.Cache; //导入依赖的package包/类
/**
     * 缓存
     */
    private void requestOkHttp() {
        new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                okhttp3.Request request = chain.request();
//                chain.request().newBuilder().addHeader().build()
                okhttp3.Response response = chain.proceed(request);
                return response;
            }
        }).cache(new okhttp3.Cache(getCacheDir(), 5 * 1024 * 1024)).build();

        okhttp3.Request request_forceNocache = new okhttp3.Request.Builder().cacheControl(new CacheControl.Builder().noCache().build()).url("").build();
        okhttp3.Request request_forceCache = new okhttp3.Request.Builder().cacheControl(new CacheControl.Builder().maxAge(0, TimeUnit.SECONDS).build()).url("").build();

    }
 
开发者ID:pop1234o,项目名称:BestPracticeApp,代码行数:19,代码来源:MainActivity.java

示例8: testCacheHeaderSerialization

import com.android.volley.Cache; //导入依赖的package包/类
public void testCacheHeaderSerialization() throws Exception {
    Cache.Entry e = new Cache.Entry();
    e.data = new byte[8];
    e.serverDate = 1234567L;
    e.ttl = 9876543L;
    e.softTtl = 8765432L;
    e.etag = "etag";
    e.responseHeaders = new HashMap<String, String>();
    e.responseHeaders.put("fruit", "banana");

    CacheHeader first = new CacheHeader("my-magical-key", e);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    first.writeHeader(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CacheHeader second = CacheHeader.readHeader(bais);

    assertEquals(first.key, second.key);
    assertEquals(first.serverDate, second.serverDate);
    assertEquals(first.ttl, second.ttl);
    assertEquals(first.softTtl, second.softTtl);
    assertEquals(first.etag, second.etag);
    assertEquals(first.responseHeaders, second.responseHeaders);
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:24,代码来源:DiskBasedCacheTest.java

示例9: makeRandomCacheEntry

import com.android.volley.Cache; //导入依赖的package包/类
/**
 * Makes a random cache entry.
 * @param data Data to use, or null to use random data
 * @param isExpired Whether the TTLs should be set such that this entry is expired
 * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
 */
public static Cache.Entry makeRandomCacheEntry(
        byte[] data, boolean isExpired, boolean needsRefresh) {
    Random random = new Random();
    Cache.Entry entry = new Cache.Entry();
    if (data != null) {
        entry.data = data;
    } else {
        entry.data = new byte[random.nextInt(1024)];
    }
    entry.etag = String.valueOf(random.nextLong());
    entry.serverDate = random.nextLong();
    entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
    entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
    return entry;
}
 
开发者ID:dreaminglion,项目名称:iosched-reader,代码行数:22,代码来源:CacheTestUtils.java

示例10: addCacheHeaders

import com.android.volley.Cache; //导入依赖的package包/类
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) {
    // If there's no cache entry, we're done.
    if (entry == null) {
        return;
    }

    if (entry.etag != null) {
        headers.put("If-None-Match", entry.etag);
    }

    if (entry.lastModified > 0) {
        Date refTime = new Date(entry.lastModified);
        headers.put("If-Modified-Since", DateUtils.formatDate(refTime));
    }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:BasicNetwork.java

示例11: parseCacheHeaders_noHeaders

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_noHeaders() {
    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertNull(entry.etag);
    assertEquals(0, entry.serverDate);
    assertEquals(0, entry.lastModified);
    assertEquals(0, entry.ttl);
    assertEquals(0, entry.softTtl);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:11,代码来源:HttpHeaderParserTest.java

示例12: parseCacheHeaders_headersSet

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_headersSet() {
    headers.put("MyCustomHeader", "42");

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertNotNull(entry.responseHeaders);
    assertEquals(1, entry.responseHeaders.size());
    assertEquals("42", entry.responseHeaders.get("MyCustomHeader"));
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:11,代码来源:HttpHeaderParserTest.java

示例13: parseCacheHeaders_etag

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_etag() {
    headers.put("ETag", "Yow!");

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertEquals("Yow!", entry.etag);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:HttpHeaderParserTest.java

示例14: parseCacheHeaders_normalExpire

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_normalExpire() {
    long now = System.currentTimeMillis();
    headers.put("Date", rfc1123Date(now));
    headers.put("Last-Modified", rfc1123Date(now - ONE_DAY_MILLIS));
    headers.put("Expires", rfc1123Date(now + ONE_HOUR_MILLIS));

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertNull(entry.etag);
    assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS);
    assertEqualsWithin(entry.lastModified, (now - ONE_DAY_MILLIS), ONE_MINUTE_MILLIS);
    assertTrue(entry.softTtl >= (now + ONE_HOUR_MILLIS));
    assertTrue(entry.ttl == entry.softTtl);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:HttpHeaderParserTest.java

示例15: parseCacheHeaders_expiresInPast

import com.android.volley.Cache; //导入依赖的package包/类
@Test public void parseCacheHeaders_expiresInPast() {
    long now = System.currentTimeMillis();
    headers.put("Date", rfc1123Date(now));
    headers.put("Expires", rfc1123Date(now - ONE_HOUR_MILLIS));

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    assertNotNull(entry);
    assertNull(entry.etag);
    assertEqualsWithin(entry.serverDate, now, ONE_MINUTE_MILLIS);
    assertEquals(0, entry.ttl);
    assertEquals(0, entry.softTtl);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:14,代码来源:HttpHeaderParserTest.java


注:本文中的com.android.volley.Cache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。