### Basic J2Cache Usage Example
Source: https://github.com/oschina/j2cache/blob/master/README.md
Demonstrates basic cache operations: setting a value, retrieving it, and evicting it. Ensure cache.close() is called upon program exit.
```java
public static void main(String[] args) {
CacheChannel cache = J2Cache.getChannel();
//缓存操作
cache.set("default", "1", "Hello J2Cache");
System.out.println(cache.get("default", "1"));
cache.evict("default", "1");
System.out.println(cache.get("default", "1"));
cache.close();
}
```
--------------------------------
### Cache Null Object with Loader
Source: https://github.com/oschina/j2cache/blob/master/CHANGES.md
Example of using the get method with a loader to cache null objects. This ensures that even if the loader returns null, it is cached, preventing repeated calls to the loader for the same key.
```java
CacheChannel cache = J2Cache.getChannel();
CacheObject obj = cache.get("Users", "13", () -> "Hello J2Cache");
System.out.println(obj);
//[Users,13,L3]=>Hello J2Cache
```
--------------------------------
### Configure Hostname Resolution in hosts file
Source: https://github.com/oschina/j2cache/blob/master/README.md
If J2Cache initialization is slow when connecting to local Redis, ensure your system's hosts file maps the machine name to the correct IP address. This example shows a typical configuration.
```text
127.0.0.1 localhost
127.0.0.1 winter-notebook.local
::1 localhost
::1 winter-notebook.local
```
--------------------------------
### Dynamically Build J2Cache Instance
Source: https://github.com/oschina/j2cache/blob/master/README.md
Shows how to programmatically create and configure a J2Cache instance using J2CacheConfig and J2CacheBuilder.
```java
J2CacheConfig config = new J2CacheConfig();
//填充 config 变量所需的配置信息
J2CacheBuilder builder = J2CacheBuilder.init(config);
CacheChannel channel = builder.getChannel();
//进行缓存的操作
channel.close();
```
--------------------------------
### Direct Access to Redis Client
Source: https://github.com/oschina/j2cache/blob/master/CHANGES.md
Demonstrates how to obtain a direct reference to the underlying Redis client for advanced operations. Ensure proper resource management by releasing the client after use.
```java
CacheChannel channel = J2Cache.getChannel();
RedisClient client = ((RedisCacheProvider)channel.getL2Provider()).getRedisClient();
try {
client.get().xxxxx(...);
} finally {
client.release();
}
```
--------------------------------
### Add Ehcache 3.x for Level 1 Cache
Source: https://github.com/oschina/j2cache/blob/master/README.md
Include this dependency to integrate Ehcache version 3.x as the Level 1 cache provider for J2Cache. Configure ehcache3.xml and place it in the classpath.
```xml
org.ehcache
ehcache
3.4.0
```
--------------------------------
### Add RocketMQ Client for Messaging
Source: https://github.com/oschina/j2cache/blob/master/README.md
Include this dependency to enable RocketMQ for message notifications within J2Cache. Configure RocketMQ properties in j2cache.properties. The scope is set to 'provided'.
```xml
org.apache.rocketmq
rocketmq-client
4.3.0
provided
```
--------------------------------
### Add Ehcache 2.x for Level 1 Cache
Source: https://github.com/oschina/j2cache/blob/master/README.md
Add this dependency to use Ehcache version 2.x as the Level 1 cache provider for J2Cache. Ensure ehcache.xml is configured and placed in the classpath.
```xml
net.sf.ehcache
ehcache
2.10.4
```
--------------------------------
### Add RabbitMQ Client for Messaging
Source: https://github.com/oschina/j2cache/blob/master/README.md
Add this dependency to use RabbitMQ for message notifications in J2Cache. Configure RabbitMQ connection details in j2cache.properties.
```xml
com.rabbitmq
amqp-client
5.3.0
```
--------------------------------
### Spring Cache Configuration with J2Cache
Source: https://github.com/oschina/j2cache/blob/master/modules/springcache/j2cache-springcache.md
This Java configuration class enables Spring Caching and sets up J2Cache as the CacheManager. It initializes J2Cache using a properties file and creates an adapter for Spring.
```java
/**
* @author Chen
*/
@Configuration
@EnableCaching
public class MyCacheConfig extends CachingConfigurerSupport {
@Override
public CacheManager cacheManager() {
// 引入配置
J2CacheConfig config = J2CacheConfig.initFromConfig("/j2cache.properties");
// 生成 J2CacheBuilder
J2CacheBuilder j2CacheBuilder = J2CacheBuilder.init(config);
// 构建适配器
J2CacheSpringCacheManageAdapter j2CacheSpringCacheManageAdapter = new J2CacheSpringCacheManageAdapter(j2CacheBuilder, true);
return j2CacheSpringCacheManageAdapter;
}
}
```
--------------------------------
### Add JGroups for Group Communication
Source: https://github.com/oschina/j2cache/blob/master/README.md
Include this dependency to enable JGroups for broadcast communication in J2Cache. This is useful in cluster environments where direct multicast might not be feasible.
```xml
org.jgroups
jgroups
3.6.13.Final
```
--------------------------------
### Add xmemcached for Memcached Level 2 Cache
Source: https://github.com/oschina/j2cache/blob/master/README.md
Add this dependency to configure Memcached as the Level 2 cache provider for J2Cache. Set 'j2cache.L2.provider_class' to 'memcached' and configure connection details in j2cache.properties.
```xml
com.googlecode.xmemcached
xmemcached
2.4.5
```
--------------------------------
### Enable Read-Only Redis Mode
Source: https://github.com/oschina/j2cache/blob/master/CHANGES.md
Configure J2Cache to use Redis in a read-only mode. This is useful for scenarios where write operations to Redis are not desired or possible.
```properties
j2cache.L2.provider_class = readonly-redis
j2cache.L2.config_section = redis
```
--------------------------------
### J2Cache Maven Dependency
Source: https://github.com/oschina/j2cache/blob/master/README_EN.md
Add the J2Cache core dependency to your Maven project to integrate the caching framework.
```xml
net.oschina.j2cache
j2cache-core
xxxxx
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.