简单的缓存池实现

package com.cache;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * 简单的缓存池实现
 * @author jianghl
 *
 */
public class SampleCachePool {

 private static SampleCachePool instance = null;

 private Map pool;

 private SampleCachePool() {
  pool = new HashMap();
 }

 public synchronized static SampleCachePool getInstance() {
  if (instance == null) {
   instance = new SampleCachePool();
  }
  return instance;
 }

 public synchronized void clearAllCacheContainer() {
  pool.clear();
 }

 public synchronized CacheContainer getCacheContainer(String name,
   boolean autoCreate) {
  if (pool.containsKey(name)) {
   return (CacheContainer) pool.get(name);
  } else if (autoCreate) {

   CacheContainer container = new CacheContainer();
   pool.put(name, container);
   return container;

  } else {
   return null;
  }
 }

 public synchronized CacheContainer getCacheContainer(String name) {
  return getCacheContainer(name, false);
 }

 public class CacheContainer {

  Map context = new HashMap();

  public Object getContent(String name) {
   if (!context.containsKey(name)) {
    return null;
   }
   CacheItem item = (CacheItem) context.get(name);
   if (item.isExpired()) {
    return null;
   } else {
    return item.getEntity();
   }

  }

  public void putContent(String name, Object obj, long expires) {

   if (!context.containsKey(name)) {
    context.put(name, new CacheItem(obj, expires));
    return;
   }
   CacheItem item = (CacheItem) context.get(name);
   item.setUpdateTime(new Date());
   item.setExpireTimes(expires);
   item.setEntity(obj);
  }

  public void putContent(String name, Object obj) {
   putContent(name, obj, -1);
  }

  private class CacheItem {
   private Date createTime = new Date();

   private Date updateTime = new Date();

   private long expireTimes = -1;

   private Object entity = null;

   public CacheItem(Object obj, long expires) {
    entity = obj;
    expireTimes = expires;
   }

   public boolean isExpired() {
    return (expireTimes != -1 && new Date().getTime()
      - updateTime.getTime() > expireTimes);
   }

   public String toString() {
    return entity.toString();
   }

   public Date getCreateTime() {
    return createTime;
   }

   public Object getEntity() {
    return entity;
   }

   public Date getUpdateTime() {
    return updateTime;
   }

   public void setCreateTime(Date date) {
    createTime = date;
   }

   public void setEntity(Object object) {
    entity = object;
   }

   public void setUpdateTime(Date date) {
    updateTime = date;
   }

   public long getExpireTimes() {
    return expireTimes;
   }

   public void setExpireTimes(long l) {
    expireTimes = l;
   }

  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
共享内存缓存是一种常见的多进程间数据共享方案,C++也提供了相应的API来进行实现。下面是一个简单的C++共享内存缓存实现: ```c++ #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <semaphore.h> using namespace std; class SharedMemory { public: SharedMemory(int size) { this->size = size; // 打开共享内存 fd = shm_open("/myshm", O_RDWR | O_CREAT, 0666); if (fd == -1) { cout << "shm_open failed" << endl; exit(-1); } // 调整共享内存大小 if (ftruncate(fd, size) == -1) { cout << "ftruncate failed" << endl; exit(-1); } // 映射共享内存 ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { cout << "mmap failed" << endl; exit(-1); } // 初始化信号量 sem_init(&sem, 1, 1); } ~SharedMemory() { // 销毁信号量 sem_destroy(&sem); // 解除共享内存映射 munmap(ptr, size); // 关闭共享内存 close(fd); // 删除共享内存 shm_unlink("/myshm"); } void* getPtr() { return ptr; } sem_t* getSem() { return &sem; } private: int fd; int size; void* ptr; sem_t sem; }; class CachePool { public: CachePool(SharedMemory& shm, int n) { this->n = n; // 初始化缓存 cache = new Cache[n]; for (int i = 0; i < n; i++) { cache[i].used = false; } // 将缓存指针保存在共享内存 memcpy(shm.getPtr(), this, sizeof(CachePool)); } ~CachePool() { delete[] cache; } void* allocate() { void* ptr = NULL; // 获取信号量 sem_t* sem = getSemaphore(); sem_wait(sem); // 查找空闲的缓存 for (int i = 0; i < n; i++) { if (!cache[i].used) { cache[i].used = true; ptr = cache[i].data; break; } } // 释放信号量 sem_post(sem); return ptr; } void free(void* ptr) { // 获取信号量 sem_t* sem = getSemaphore(); sem_wait(sem); // 查找对应的缓存 for (int i = 0; i < n; i++) { if (cache[i].data == ptr) { cache[i].used = false; break; } } // 释放信号量 sem_post(sem); } private: struct Cache { bool used; char data[1024]; }; int n; Cache* cache; sem_t* getSemaphore() { // 获取共享内存的信号量指针 SharedMemory* shm = (SharedMemory*)mmap(NULL, sizeof(SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); sem_t* sem = shm->getSem(); munmap(shm, sizeof(SharedMemory)); return sem; } }; int main() { // 创建共享内存 SharedMemory shm(sizeof(CachePool)); // 创建缓存 CachePool* pool = new(shm.getPtr()) CachePool(shm, 10); // 在缓存分配内存 char* p = (char*)pool->allocate(); strcpy(p, "hello world"); cout << p << endl; // 释放内存 pool->free(p); return 0; } ``` 以上代码实现了一个简单的共享内存缓存,使用了C++的类和对象来封装共享内存和缓存,并使用了信号量来实现多进程之间的同步。需要注意的是,由于共享内存和信号量是操作系统的资源,因此在使用完毕之后需要及时释放,否则可能会导致系统资源的浪费。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值