1 2 3 | import com.alibaba.appengine.api.cache.CacheService; import com.alibaba.appengine.api.cache.CacheServiceFactory; CacheService cacheService = CacheServiceFactory.getCacheService(alias); |
1 2 3 4 5 6 7 8 9 10 11 | /** * 通过缓存key,关联被缓存的文本内容 * 如果缓存key已经存在于缓存中,方法调动将会覆盖之前的关联的缓存内容 * 相当于set(key,value,NEVER_EXPIRE) * @param key 缓存key,不能为空null * @param value 缓存内容,不能为空null * @return 如果关联的缓存成功的话,返回true,否则返回false * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key或value为null */ boolean put(String key, Serializable value); |
1 2 3 4 5 6 7 8 9 10 11 | /** * 通过缓存key,关联被缓存的文本内容 * 如果缓存key已经存在于缓存中,方法调动将会覆盖之前的关联的缓存内容 * @param key 缓存key,不能为空null * @param value 缓存内容,不能为空null * @param expire 过期时长,单位为秒。小于等于0的值,表示永不失效,推荐使用常量{@link #NEVER_EXPIRE}。 * @return 如果关联的缓存成功的话,返回true,否则返回false * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key或value为null */ boolean put(String key, Serializable value, int expire); |
1 2 3 4 5 6 7 8 9 | /** *如果没有原来的值没有被更新,则去更新,否则不做更新;即CAS操作。 * @param key 缓存key,不能为空null * @param identifiableValue - 原来的CacheService.IdentifiableValue * @param newValue - 新值 * @param expire - 过期时长,单位为秒。小于等于0的值,表示永不失效,推荐使用常量NEVER_EXPIRE。 * @return 如果关联的缓存成功的话,返回true,否则返回false */ CacheService.IdentifiableValue putIfUntouched(String key, Serializable value, Serializable newValue, int expire); |
1 2 3 4 5 6 7 8 | /** * 通过key,获取其关联缓存的内容 * @param key 缓存key,不能为空null * @return 如果没有关联缓存,或缓存的内容已过期的话,返回为null * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Serializable get(String key); |
1 2 3 4 5 6 7 8 | /** * 一次get多个Key * @param key keys - Key集合 * @return 包含找到的Key-Value对的Map * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数为null */ Map getAll(String key); |
1 2 3 4 5 6 7 | /** * 和get(String)类似,返回的CacheService.IdentifiableValue对象给putIfUntouched(java.lang.String, com.alibaba.appengine.api.cache.CacheService.IdentifiableValue, java.io.Serializable, int)方法使用 * @param key 缓存key,不能为空null * @return 返回CacheService.IdentifiableValue * @throws CacheException 操作超时,或是Cache出错,比如连接失败 */ CacheService.IdentifiableValue getIdentifiable(String key); |
1 2 3 4 5 6 7 8 | /** * 删除缓存key关联的缓存内容 * @param key 缓存key,不能为空null * @return 如果删除关联的缓存成功的话,返回true,否则返回false * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ boolean delete(String key); |
1 2 3 4 5 6 7 8 | /** * 删除缓存key关联的缓存内容 * @param keys 缓存key集合,不能为空null * @return 如果删除成功的key的集合 * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Set deleteAll(String keys); |
1 2 3 4 5 6 7 8 9 | /** * 递增Key的值 * @param key 缓存key集合,不能为空null * @param delta 递增步长。可以是负数或正数 * @return 返回递增之后的值 * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Long increment(String key, long delta) |
1 2 3 4 5 6 7 8 9 10 | /** * 递增Key的值 * @param key 缓存key集合,不能为空null * @param delta 递增步长。可以是负数或正数 * @param initialValue 如果Key不存在,则这个初始值。 * @return 返回递增之后的值 * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Long increment(String key, long delta, long initialValue) |
1 2 3 4 5 6 7 8 9 | /** * 递减Key的值 * @param key 缓存key集合,不能为空null * @param delta 递减步长。可以是负数或正数 * @return 返回递减之后的值。 * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Long decrement(String key, long delta) |
1 2 3 4 5 6 7 8 9 10 | /** * 递减Key的值 * @param key 缓存key集合,不能为空null * @param delta 递减步长。可以是负数或正数 * @param initialValue 如果Key不存在,则这个初始值。 * @return 返回递减之后的值 * @throws CacheException 操作超时,或是Cache出错,比如连接失败 * @throws NullPointerException 参数key为null */ Long decrement(String key, long delta, long initialValue) |
>> Begin cache service test.
Save value FooKey to key cache-value.
Read value FooKey to key cache-value.
Cache service test ok!
Save value UserKey to key User{name='Shylock', sex=true, age=20, weight=150}.
cache put result:true
Read value UserKey to key User{name='Shylock', sex=true, age=20, weight=150}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | CacheService cacheService = CacheServiceFactory.getCacheService(); // Test String final String key = "FooKey" ; final String value = "cache-value" ; resp.getWriter().printf( "Save value %s to key %s.\n" , key, value); cacheService.put(key, value); String get = (String) cacheService.get(key); resp.getWriter().printf( "Read value %s to key %s.\n" , key, get); if (value.equals(get)) { resp.getWriter().println( "Cache service test ok!" ); } else { resp.getWriter().printf( "Fail to get, expect %s, actual %s!\n" , value, get); } // Test Pojo final User user = new User(); user.setName( "Shylock" ); user.setSex( true ); user.setAge(( byte ) 20 ); user.setWeight( 150 ); final String cacheKey = "UserKey" ; resp.getWriter().printf( "Save value %s to key %s.\n" , cacheKey, user); boolean success = cacheService.put(cacheKey, user); resp.getWriter().println( "cache put result:" + success); User cacheUser = (User) cacheService.get(cacheKey); resp.getWriter().printf( "Read value %s to key %s.\n" , cacheKey, cacheUser); |
1 2 3 4 5 6 7 8 9 | < servlet > < servlet-name >CacheServiceDemo</ servlet-name > < servlet-class >tae.demo.services.CacheServiceDemo</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >CacheServiceDemo</ servlet-name > < url-pattern >/CacheServiceDemo.do</ url-pattern > </ servlet-mapping > |
>> Begin cache service test.
Save value FooKey to key cache-value.
Read value FooKey to key cache-value.
Cache service test ok!
Save value UserKey to key User{name='Shylock', sex=true, age=20, weight=150}.
cache put result:true
Read value UserKey to key User{name='Shylock', sex=true, age=20, weight=150}.